Django Module 8
Class-based views interview questions
Interview questions on class-based views from Django: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What does as_view() return?
-
Fresher to mid
Why must LoginRequiredMixin come before the view class?
-
Fresher
What does paginate_by add to a ListView's context?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A code review finds class ReportView(TemplateView, LoginRequiredMixin) in a project with 80 class-based views. The page has been in production for months. What is the impact, how do you check the other 79 views, and what stops it from happening again?
- What it tests
- Reading the MRO to see that a mixin never runs, judging exposure, finding the pattern across a codebase, and guarding it with tests or a check that fail on the wrong order.
- First step
- Confirm the impact before anything else: request the page without signing in and expect 302; with this order it answers 200, because View.dispatch() comes before the mixin in the MRO and never calls super(). Then fix this view and look at what the page showed.
- Follow-ups
-
- How do you find every view with an access mixin after its view class?
- What does a test for the whole project look like?
- Would LoginRequiredMiddleware have prevented it?
From the lesson Class-based views and mixins
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
Half of a team writes function views and half writes class-based views, and code reviews keep turning into arguments. You are asked to write the team's rule. What do you decide, what do you leave open, and how do you make the rule checkable?
- What it tests
- Judging the real trade-offs of both kinds, deciding by reuse and readability rather than taste, and turning a style rule into checks a review or CI can apply.
- First step
- List what actually goes wrong in the arguments: duplicated access checks, deep mixin stacks nobody can read, or style only. The rule should fix those problems, and leave alone choices that do not cause any.
- Follow-ups
-
- Where do generic views fit in the rule?
- How do you handle access checks the same way in both kinds?
- What about async views?
From the lesson Class-based views and mixins
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to senior
An UpdateView for complaints has model = Complaint and fields = ['category', 'description'] behind LoginRequiredMixin. A resident shows that she can edit any flat's complaint by changing the number in the URL. Fix the edit and delete pages, keep them consistent with the list, and say what you test.
- What it tests
- How generic views find the object through get_queryset(), scoping editing more tightly than viewing, where access rules belong in the request flow, and a test matrix that fails on the weak view.
- First step
- Explain the hole first: with only model set, get_queryset() is every complaint, and get_object() looks the pk up there, so LoginRequiredMixin's check that someone is signed in is the only check. Then decide who may edit: the whole flat, or only the reporter while the complaint is open.
- Follow-ups
-
- Why not check the owner in form_valid() instead?
- Should another resident get 403 or 404?
- How does the delete page stay consistent?
From the lesson Generic views and pagination
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
The complaints list of a deployment that serves 400 societies has 2 million rows. Page 1 is quick, and page 20,000 takes seconds. What is happening, and what do you change?
- What it tests
- Why deep pages of a large table get slow, keyset pagination and its trade-offs, and when an exact count is worth paying for.
- First step
- Capture the queries of one page view, for page 1 and for page 20,000, and compare them: look at how many rows each query must read, not at how many it returns. That decides whether you need an index, another way to paginate, or both.
- Follow-ups
-
- What does keyset pagination give up?
- How do you show "about 2 million complaints" without counting them?
- Which index does the keyset query need?
From the lesson Generic views and pagination
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
The complaints list can be filtered by category and is paginated. Residents report that the filter is lost when they go to page 2, and that some complaints appear on two pages while others appear on none. Explain both bugs and fix them.
- What it tests
- Keeping query parameters in page links, a stable order for paginated lists, and tests that catch a row shown on two pages.
- First step
- Look at the page links: ?page=2 alone drops category=lift, so page 2 is a page of the unfiltered list. Then treat the repeated and missing complaints as a second bug: check what the list is sorted by, and whether two complaints can sort as equal.
- Follow-ups
-
- How do you build page links that keep every other parameter?
- Which ordering makes pages stable?
- How do you test it?
From the lesson Generic views and pagination
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes