What are Django views — function-based vs class-based?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Django basics — a prerequisite for any developer role.
Answer
Django views receive HTTP requests and return HTTP responses. Two styles: Function-Based Views (FBV): simple Python functions. from django.shortcuts import render, get_object_or_404 from django.http import JsonResponse def article_detail(request, pk): article = get_object_or_404(Article, pk=pk, is_published=True) if request.method == "POST": # handle form submission pass return render(request, "articles/detail.html", {"article": article}). Pros: simple, explicit, easy to understand. Cons: code duplication for common patterns. Class-Based Views (CBV): use Python classes with HTTP method handlers. Django provides generic CBVs for common patterns: from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView class ArticleListView(ListView): model = Article template_name = "articles/list.html" context_object_name = "articles" paginate_by = 10 def get_queryset(self): return Article.objects.filter(is_published=True) class ArticleCreateView(LoginRequiredMixin, CreateView): model = Article fields = ["title", "content"] success_url = "/articles/". Generic CBVs: ListView, DetailView, CreateView, UpdateView, DeleteView, FormView, TemplateView, RedirectView, LoginView, LogoutView. Pros: reuse code via inheritance and mixins; less boilerplate for CRUD. Cons: can be harder to understand the flow. When to use each: FBVs for complex logic with many conditions; CBVs for standard CRUD and REST APIs. Mixins like LoginRequiredMixin, PermissionRequiredMixin add cross-cutting behavior.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Django codebase.