Sample questions
Question 1Easy
Which mixin restricts a CBV to logged-in users?
- AuthOnlyMixin
- UserMixin
- LoginRequiredMixin
- SessionMixin
Answer: C. LoginRequiredMixin
Explanation
Put the mixin first in the base-class list so it runs before the view.
Step-by-step solution
- Add to bases leftmost.
- 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?
- IndexView
- ObjectsView
- TableView
- ListView
Answer: D. ListView
Explanation
Set model or queryset; the template defaults to <app>/<model>_list.html.
Step-by-step solution
- Set model.
- 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?
- DetailView
- SingleView
- ItemView
- GetView
Answer: A. DetailView
Explanation
DetailView fetches the object and puts it in context as object and the model name.
Step-by-step solution
- URL has pk.
- 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?
- django.views.Base
- django.views.View
- django.http.View
- django.db.View
Answer: B. django.views.View
Explanation
Subclass View and define get(), post(), and so on.
Step-by-step solution
- Subclass View.
- 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?
- CreateView
- MakeView
- FormCreator
- 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
- GET shows form.
- 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?
- A redirect written inside the template, which the generic view then follows
- success_url or get_absolute_url on the object
- A signal
- 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
- Save object.
- 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?
- path('x/', MyView)
- path('x/', MyView), passing the class itself, which Django calls like a function
- path('x/', MyView.as_view())
- 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
- Call as_view.
- 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?
- Saving the model instance to the database before the template is rendered
- Adding extra variables to the template context; call super() first
- Choosing the template
- 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
- Call super.
- Add keys.
- 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?
- Alphabetical order, so LoginRequiredMixin always sorts ahead of every other base
- Because ListView must be last by rule of Django
- So it can import faster
- 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
- MRO left to right.
- 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 →