Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

8 questions: 3 warm-ups and 5 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What does as_view() return?

    Answered in the lesson Class-based views and mixins

  2. Fresher to mid

    Why must LoginRequiredMixin come before the view class?

    Answered in the lesson Class-based views and mixins

  3. Fresher

    What does paginate_by add to a ListView's context?

    Answered in the lesson Generic views and pagination

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. 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

    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.

    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
  2. 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

    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
  3. 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

    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
  4. 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

    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
  5. 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

    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

All Django interview questions · The Django track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.