Django MCQS

Q. Django is written in which programming language?

A
Java
B
Python
C
JavaScript
D
PHP
Solution:

Django is a Python web framework; models, views and settings are all Python code.

Q. Which architectural pattern does Django follow?

A
MVVM
B
MVT (Model-View-Template)
C
Flux
D
Strict MVC with controllers.py
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?

A
django-admin startproject blog
B
python manage.py startapp blog
C
pip install blog
D
python manage.py createapp blog
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?

A
/dashboard/
B
/django/
C
/admin/
D
/manage/
Solution:

The default project urls.py maps admin/ to admin.site.urls.

Q. Which command applies pending migrations to the database?

A
makemigrations
B
migrate
C
sqlmigrate
D
collectstatic
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?

A
{% load static %}
B
{% csrf_token %}
C
{{ form.media }}
D
{% block content %}
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?

A
Router
B
Middleware
C
Serializer
D
Signal
Solution:

Serializers handle both serialization (object to JSON) and deserialization with validation.

Q. What does null=True do on a model field?

A
Allows the field to be left empty in forms
B
Allows the database column to store NULL
C
Makes the field optional in the admin only
D
Deletes the field when empty
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?

A
As soon as .filter() is called
B
When it is evaluated, for example iterated, sliced with a step, or passed to list()
C
Only when .save() is called
D
Every time a template is loaded
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?

A
It is faster at runtime
B
Class attributes run at import time, before the URLconf is loaded
C
reverse() does not support named URLs
D
reverse_lazy() works without a URL name
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?

A
A dictionary
B
An HttpResponse object (or raise an exception)
C
A template file path
D
A QuerySet
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?

A
self
B
The URL pattern
C
The HttpRequest object
D
The template name
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?

A
import views
B
from . import views
C
from django import views
D
include(views)
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?

A
views.PostList
B
views.PostList()
C
views.PostList.as_view()
D
views.PostList.render()
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?

A
ListView
B
DetailView
C
TemplateView
D
FormView
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?

A
Returns None
B
Raises Http404, showing a 404 page
C
Creates a new Post
D
Raises a 500 server error
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?

A
Add paginate_by
B
Use select_related("author") on the queryset
C
Use prefetch_related("title")
D
Cache the template
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?

A
After the generic view: class V(ListView, LoginRequiredMixin)
B
First (leftmost): class V(LoginRequiredMixin, ListView)
C
It must be applied as a decorator only
D
Inside the Meta class
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?

A
{% title %}
B
{{ title }}
C
<%= title %>
D
${title}
Solution:

Double curly braces output a variable; {% %} is reserved for tags.

Q. Which shortcut combines a template with context and returns an HttpResponse?

A
redirect()
B
render()
C
reverse()
D
include()
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?

A
{% include static %}
B
{% load static %}
C
{% extends static %}
D
{% import static %}
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?

A
ENGINE, PATHS, DEBUG, CONTEXT
B
BACKEND, DIRS, APP_DIRS, OPTIONS
C
LOADER, ROOT, STATIC, MEDIA
D
NAME, URL, FILES, OPTIONS
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?

A
forloop.index
B
forloop.counter
C
loop.index
D
forloop.counter0
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?

A
{% static post.cover %}
B
C
D
{% media post.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?

A
static/admin/base_site.html
B
templates/admin/base_site.html with the templates folder in DIRS
C
blog/admin.py
D
templates/base_site.html
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?

A
It is rendered at the top of the page
B
It raises TemplateSyntaxError
C
It is ignored
D
It replaces the base template
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?

A
A user's uploaded profile photo
B
The site's style.css
C
A row in the database
D
The settings.py file
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?

A
django.contrib.admin
B
django.contrib.staticfiles
C
django.contrib.media
D
django.contrib.assets
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 %}?

A
{% load static %}
B
{% static on %}
C
{% include "static" %}
D
Nothing
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?

A
Deletes unused static files
B
Copies static files from all source folders into STATIC_ROOT
C
Minifies CSS automatically
D
Uploads media files to the database
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?

A
STATIC_ROOT
B
STATIC_URL
C
STATICFILES_DIRS
D
MEDIA_ROOT
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?

A
Django requires three levels of folders
B
To avoid collisions when two apps have files with the same name
C
To make collectstatic faster
D
To hide the files from users
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?

A
Set STATICFILES_STORAGE = "...ManifestStaticFilesStorage"
B
Set the "staticfiles" key in the STORAGES setting
C
Set STATIC_HASH = True
D
Pass --hash to runserver
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?

A
Django serves them normally
B
They return 404 because Django no longer serves static files
C
They redirect to STATIC_ROOT
D
Django raises ImproperlyConfigured on startup
Solution:

 Static serving in runserver is a development convenience; production needs a web server, CDN or WhiteNoise.

Entri PDF Icon

Get Question Bank

Strengthen Your Practice with our comprehensive question bank.