Django MCQS
Q. Django is written in which programming language?
Solution:
Django is a Python web framework; models, views and settings are all Python code.
Q. Which architectural pattern does Django follow?
Solution:
Django calls its pattern MVT: models for data, views for logic and templates for presentation, with the URL dispatcher acting as controller.
Q. Which command creates a new app inside an existing Django project?
Solution:
startapp (run through manage.py) creates an app package. startproject creates the whole project.
Q. By default, at which URL path is the Django admin available?
Solution:
The default project urls.py maps admin/ to admin.site.urls.
Q. Which command applies pending migrations to the database?
Solution:
makemigrations only writes migration files; migrate executes them against the database.
Q. What must a Django template include inside every POST form to pass CSRF protection?
Solution:
{% csrf_token %} inserts the hidden token that CsrfViewMiddleware checks.
Q. In Django REST Framework, which component converts model instances into JSON-ready data and validates incoming data?
Solution:
Serializers handle both serialization (object to JSON) and deserialization with validation.
Q. What does null=True do on a model field?
Solution:
null=True is a database-level setting. Form-level emptiness is controlled by blank=True.
Q. When does a Django QuerySet actually hit the database?
Solution:
QuerySets are lazy; chaining filters builds the query, and evaluation triggers the SQL.
Q. Why is reverse_lazy() used instead of reverse() for success_url on a class-based view?
Solution:
Class bodies execute when the module is imported; reverse_lazy defers URL resolution until the value is actually used.
Q. What must every Django view return?
Solution:
Django requires a response object; render(), redirect() and JsonResponse all return HttpResponse subclasses.
Q. What is the first argument passed to a function-based view?
Solution:
Django calls every function-based view with request first, followed by any URL kwargs.
Q. Which line correctly imports an app's views inside that app's urls.py?
Solution:
A relative import (from . import views) loads views.py from the same app package.
Q. How is a class-based view referenced in a URL pattern?
Solution:
as_view() returns a callable function that Django’s URL resolver can invoke.
Q. Which generic view is designed to display a single object?
Solution:
DetailView fetches one object using pk or slug from the URL.
Q. What does get_object_or_404(Post, slug=slug) do when no post matches?
Solution:
t wraps get() and converts DoesNotExist into Http404.
Q. A post list template shows each post's author name and runs 51 queries for 50 posts. What is the best fix?
Solution:
select_related joins the ForeignKey in one query, removing the N+1 pattern.
Q. Where should LoginRequiredMixin go in a class-based view definition?
Solution:
Python’s method resolution order applies left to right, so the mixin must come first to run its dispatch check before the view.
Q. Which syntax outputs a variable in a Django template?
Solution:
Double curly braces output a variable; {% %} is reserved for tags.
Q. Which shortcut combines a template with context and returns an HttpResponse?
Solution:
django.shortcuts.render(request, template_name, context) loads, renders and wraps the template in a response.
Q. Which tag must appear before you can use {% static %} in a template?
Solution:
The static tag lives in a template library that must be loaded in every template that uses it.
Q. Which keys does an entry in the TEMPLATES setting accept?
Solution:
BACKEND, DIRS, APP_DIRS and OPTIONS are the documented keys; engine settings go inside OPTIONS.
Q. In a for loop, which variable gives the 1-based iteration number?
Solution:
forloop.counter starts at 1; forloop.counter0 starts at 0.
Q. How do you display a user-uploaded image stored in an ImageField named cover?
Solution:
Uploaded files are media, not static; .url returns the public URL built from MEDIA_URL.
Q. To override the Django admin's branding template project-wide, where should you place your file?
Solution:
The override must mirror the admin’s path, admin/base_site.html, inside a folder the loader searches before the admin app.
Q. A child template has text outside any {% block %} tag. What happens to that text?
Solution:
In a template that uses {% extends %}, only content inside blocks defined in the parent is rendered.
Q. Which of these is a static file in a Django project?
Solution:
Static files are developer-supplied assets like CSS, JS and images; user uploads are media files.
Q. Which built-in app handles static files?
Solution:
django.contrib.staticfiles provides the {% static %} tag and the collectstatic command.
Q. What must you add at the top of a template before using {% static %}?
Solution:
The static tag belongs to a library that must be loaded in each template that uses it.
Q. What does python manage.py collectstatic do?
Solution:
It gathers files from apps and STATICFILES_DIRS into one folder for a web server or CDN to serve.
Q. Which setting lists extra project-level folders that Django searches for static files?
Solution:
STATICFILES_DIRS adds source locations beyond each app’s static/ folder.
Q. Why namespace app static files as blog/static/blog/css/style.css?
Solution:
All static files share one namespace after collection, so the app-name folder keeps paths unique.
Q. In Django 5.1 and later, how do you configure hashed static filenames?
Solution:
STATICFILES_STORAGE was removed in Django 5.1; storage backends are now configured through STORAGES.
Q. With DEBUG = False and no other configuration, what happens to requests for /static/style.css?
Solution:
Static serving in runserver is a development convenience; production needs a web server, CDN or WhiteNoise.
Get Question Bank
Strengthen Your Practice with our comprehensive question bank.