Django Interview Questions and Answers (2 Years Experience)
1. Explain Django's MVT architecture. How does it differ from MVC?
Answer: Django follows the MVT (Model-View-Template) architecture:
- Model: Represents the data and business logic (database layer).
- View: Handles request/response and contains business logic.
- Template: Responsible for rendering HTML content (UI).
Difference from MVC: In MVC, View = Django Template, and Controller = Django View. Django
handles controller-like tasks internally.
2. What is a Django model?
Answer: A model is a Python class that maps to a database table.
Example:
class Book(models.Model):
title = models.CharField(max_length=100)
published_date = models.DateField()
3. What is the difference between null=True and blank=True?
Answer:
- null=True: Database-level stores NULL in the database.
- blank=True: Form-level field is allowed to be empty in forms.
4. What is the difference between makemigrations and migrate?
Answer:
- makemigrations: Creates migration files based on model changes.
- migrate: Applies those migration files to the database.
5. What are Django signals?
Answer: Signals notify certain actions like model save.
Example:
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
6. What is a ModelForm?
Answer: A ModelForm automatically creates a form from a model.
Example:
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'published_date']
7. How does template inheritance work in Django?
Answer: Use {% block %} in base.html and override in child templates.
Example:
{% extends "base.html" %}
{% block content %}
<h1>Welcome</h1>
{% endblock %}
8. What is the difference between select_related and prefetch_related?
Answer:
- select_related: Joins using SQL (for FK relationships).
- prefetch_related: Separate queries, joins in Python (for M2M or reverse FK).
9. How do you handle static files and media in Django?
Answer:
In settings.py:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In urls.py:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
10. How do you implement login and logout in Django?
Answer:
Use built-in views:
path('login/', auth_views.LoginView.as_view(), name='login')
path('logout/', auth_views.LogoutView.as_view(), name='logout')
11. How do you create a custom middleware?
Answer:
class MyMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
print("Before view")
response = self.get_response(request)
print("After view")
return response
12. What is a Serializer in Django REST Framework?
Answer: Translates model instances/data to JSON and vice versa.
Example:
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
13. How do you customize the Django admin panel?
Answer:
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'published_date')
search_fields = ('title',)
admin.site.register(Book, BookAdmin)
14. Explain the use of the Meta class in Django models.
Answer: Used for metadata like ordering, verbose names.
Example:
class Book(models.Model):
title = models.CharField(max_length=100)
class Meta:
ordering = ['title']
15. What is the purpose of Django's CSRF protection?
Answer: Prevents malicious POST requests. Use {% csrf_token %} in forms.