Class-based Views Questions with Answers

9 practice questions on Class-based Views (Django) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Easy

Which mixin restricts a CBV to logged-in users?

  1. AuthOnlyMixin
  2. UserMixin
  3. LoginRequiredMixin
  4. SessionMixin

Answer: C. LoginRequiredMixin

Explanation

Put the mixin first in the base-class list so it runs before the view.

Step-by-step solution

  1. Add to bases leftmost.
  2. Anonymous users redirect to login.

Why the other options are wrong

  • A. AuthOnlyMixin is not a Django class; the correct choice is LoginRequiredMixin.
  • B. UserMixin is not a Django class; the correct choice is LoginRequiredMixin.
  • D. SessionMixin is not a Django class; the correct choice is LoginRequiredMixin.

Question 2Easy

Which generic view lists model objects and exposes object_list?

  1. IndexView
  2. ObjectsView
  3. TableView
  4. ListView

Answer: D. ListView

Explanation

Set model or queryset; the template defaults to <app>/<model>_list.html.

Step-by-step solution

  1. Set model.
  2. Template name is derived.

Why the other options are wrong

  • A. IndexView is not one of Django's generic views; the correct choice is ListView.
  • B. ObjectsView is not one of Django's generic views; the correct choice is ListView.
  • C. TableView is not one of Django's generic views; the correct choice is ListView.

Question 3Easy

Which generic view shows one object looked up by pk or slug?

  1. DetailView
  2. SingleView
  3. ItemView
  4. GetView

Answer: A. DetailView

Explanation

DetailView fetches the object and puts it in context as object and the model name.

Step-by-step solution

  1. URL has pk.
  2. Object in context.

Why the other options are wrong

  • B. SingleView is not one of Django's generic views; the correct choice is DetailView.
  • C. ItemView is not one of Django's generic views; the correct choice is DetailView.
  • D. GetView is not one of Django's generic views; the correct choice is DetailView.

Question 4Easy

Which base class gives a simple view method per HTTP verb?

  1. django.views.Base
  2. django.views.View
  3. django.http.View
  4. django.db.View

Answer: B. django.views.View

Explanation

Subclass View and define get(), post(), and so on.

Step-by-step solution

  1. Subclass View.
  2. Define verb methods.

Why the other options are wrong

  • A. django.views.Base is not a Django class; the correct choice is django.views.View.
  • C. django.http.View is not a Django class; the correct choice is django.views.View.
  • D. django.db.View is not a Django class; the correct choice is django.views.View.

Question 5Easy

Which generic view handles a ModelForm to create an object?

  1. CreateView
  2. MakeView
  3. FormCreator
  4. InsertView

Answer: A. CreateView

Explanation

CreateView shows the form on GET and saves on valid POST, then redirects to success_url.

Step-by-step solution

  1. GET shows form.
  2. POST saves.

Why the other options are wrong

  • B. MakeView is not one of Django's generic views; the correct choice is CreateView.
  • C. FormCreator is not one of Django's generic views; the correct choice is CreateView.
  • D. InsertView is not one of Django's generic views; the correct choice is CreateView.

Question 6Medium

What must CreateView have to know where to go after success?

  1. A redirect written inside the template, which the generic view then follows
  2. success_url or get_absolute_url on the object
  3. A signal
  4. A middleware

Answer: B. success_url or get_absolute_url on the object

Explanation

Without success_url, Django uses object.get_absolute_url() or raises an error.

Step-by-step solution

  1. Save object.
  2. Resolve success URL.

Why the other options are wrong

  • A. Redirects are not template-level.
  • C. Signals do not redirect.
  • D. Middleware is not used.

Question 7Medium

How do you use a class-based view in urls.py?

  1. path('x/', MyView)
  2. path('x/', MyView), passing the class itself, which Django calls like a function
  3. path('x/', MyView.as_view())
  4. path('x/', MyView.run)

Answer: C. path('x/', MyView.as_view())

Explanation

as_view() returns a function that builds a fresh instance per request.

Step-by-step solution

  1. Call as_view.
  2. Pass result to path.

Why the other options are wrong

  • A. URLs need a callable view function.
  • B. Calling creates an instance, not a view.
  • D. run is not a method.

Question 8Hard

What is the role of get_context_data() in a CBV?

  1. Saving the model instance to the database before the template is rendered
  2. Adding extra variables to the template context; call super() first
  3. Choosing the template
  4. Setting the HTTP status

Answer: B. Adding extra variables to the template context; call super() first

Explanation

Override it, call super().get_context_data(**kwargs), add your keys and return.

Step-by-step solution

  1. Call super.
  2. Add keys.
  3. Return context.

Why the other options are wrong

  • A. Saving happens in form_valid.
  • C. template_name does that.
  • D. Response class does that.

Question 9Hard

In class V(LoginRequiredMixin, ListView), why is the mixin placed first?

  1. Alphabetical order, so LoginRequiredMixin always sorts ahead of every other base
  2. Because ListView must be last by rule of Django
  3. So it can import faster
  4. Python's method resolution order makes its dispatch run before ListView's

Answer: D. Python's method resolution order makes its dispatch run before ListView's

Explanation

The mixin's dispatch checks authentication and then calls super().

Step-by-step solution

  1. MRO left to right.
  2. Mixin dispatch first.

Why the other options are wrong

  • A. MRO is not alphabetical.
  • B. The rule is about MRO, not Django registry.
  • C. Import speed is unaffected.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Django topics · All topics