What is Django Generic Views and Mixins?
Why Interviewers Ask This
This question targets practical, hands-on experience with Django. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
Django's generic class-based views provide reusable base classes for common patterns, reducing boilerplate. Display views: class ArticleListView(ListView): model = Article template_name = "articles/list.html" context_object_name = "articles" paginate_by = 20 queryset = Article.objects.filter(is_published=True).select_related("author") ordering = ["-published_at"] def get_queryset(self): qs = super().get_queryset() category = self.request.GET.get("category") if category: qs = qs.filter(category__slug=category) return qs def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["categories"] = Category.objects.all() return context. Edit views: class ArticleCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView): model = Article form_class = ArticleForm template_name = "articles/create.html" success_url = reverse_lazy("article-list") success_message = "Article created successfully!" def form_valid(self, form): form.instance.author = self.request.user return super().form_valid(form). Mixins for access control: LoginRequiredMixin — redirect to login if not authenticated; PermissionRequiredMixin — require specific permission; UserPassesTestMixin — custom test: def test_func(self): return self.request.user == self.get_object().author. Common mixins: SuccessMessageMixin, MultipleObjectMixin, SingleObjectMixin. Method resolution order (MRO): order of mixins matters — Django uses Python MRO for method resolution. Place LoginRequiredMixin before the view class.
Common Mistake
A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.