Django Interview Questions

2. What’s new in the latest Django versions (e.g., 4.x and 5.x)?

  • Async view support
  • Type annotations in views and models
  • Redis cache backend enhancements
  • Better ORM performance
  • Support for Python 3.12
  • Streaming HTTP responses with async views

3. What is the Django MVT architecture?

  • Model: Defines the data structure (database schema)
  • View: Handles business logic and user requests
  • Template: Handles presentation logic (HTML rendering)

4. How does Django ORM work?

Django ORM maps Python classes to database tables. It allows developers to write database queries using Python methods instead of raw SQL.

User.objects.filter(is_active=True)

5. What is a QuerySet in Django?

A QuerySet is a collection of database queries constructed using Django’s ORM. It’s lazy, meaning it doesn’t hit the DB until needed.

6. What is a middleware in Django?

Middleware is a layer that processes requests and responses globally before reaching views or returning to the browser. Examples include authentication, session, and security middlewares.

7. How do you implement custom user authentication in Django?

By extending AbstractBaseUser and BaseUserManager, and updating AUTH_USER_MODEL in settings.py.

8. What is the difference between null=True and blank=True in models?

  • null=True: Database can store NULL (used at DB level)
  • blank=True: Field can be left empty in forms (used at validation level)

9. How are static files and media files managed in Django?

  • Static files (CSS, JS): Managed with STATIC_URL, STATICFILES_DIRS
  • Media files (user uploads): Managed with MEDIA_URL, MEDIA_ROOT

10. How is Django REST Framework (DRF) used?

DRF is used to build Web APIs in Django. It provides:

  • Serializers for converting data
  • ViewSets and Routers
  • Built-in authentication
  • Browsable API interface

11. What is the difference between function-based views (FBV) and class-based views (CBV)?

  • FBV: Simple and explicit
  • CBV: More abstract, reusable, supports mixins and inheritance
  • class MyView(View):     
         Def get(self, request): ... 

12. How does Django handle form validation?

Using Form and ModelForm classes which validate user input through the clean() and clean_() methods.

13. What is the use of signals in Django?

Signals allow decoupled components to get notified when actions occur (e.g., post_save, pre_delete). They are useful for side effects like sending emails.

14. What are Django’s built-in security features?

  • CSRF protection
  • XSS and clickjacking protection
  • SQL injection prevention via ORM

    15. How does Django handle asynchronous views?

    Since Django 3.1+, views can be defined using async def, allowing non-blocking I/O and integration with asyncio, useful for APIs and websockets.

    16. How is caching implemented in Django?

    Django supports various cache backends like:

    • In-memory (locmem)
    • Memcached
    • Redis

    Use cache.set() and cache.get() or per-view caching decorators.

    17. How do you perform database migrations in Django?

    Use:

    python manage.py makemigrations 
    Python manage.py migrate 

    This creates and applies schema changes to the database.

    18. What is the role of the admin interface in Django?

    It provides an auto-generated, customizable interface to manage site data based on registered models — useful for internal tools or CMS.

    19. What is a context processor?

    A context processor is a function that injects global variables into templates, such as current user or site settings.

    20. How do you deploy Django to production?

    • Use WSGI/ASGI (e.g., with Gunicorn or Daphne)
    • Set DEBUG = False
    • Use a production-ready database (e.g., PostgreSQL)
    • Serve static/media files via Nginx
    • Use environment variables for secrets
    • Secure with HTTPS and proper headers