Sample questions
Question 1Easy
Which shortcut renders a template and returns an HttpResponse?
- display(request)
- html(request, 'page.html'), a shortcut that returns the raw file as a response
- render(request, 'page.html', context)
- template(request)
Answer: C. render(request, 'page.html', context)
Explanation
render loads the template, fills the context and returns a response.
Step-by-step solution
- Provide request, template, context.
- Get a response.
Why the other options are wrong
- A.
display(request) is not a Django shortcut; the correct choice is render(request, 'page.html', context). - B. html() is not a Django shortcut; render() loads, fills and returns the template.
- D.
template(request) is not a Django shortcut; the correct choice is render(request, 'page.html', context).
Question 2Easy
What is the first parameter of every function-based view?
- response
- request
- self
- context
Answer: B. request
Explanation
Django passes an HttpRequest object to the view.
Step-by-step solution
- URL matched.
- View called with request.
Why the other options are wrong
- A. Responses are returned.
- C. Only methods have self.
- D. Context goes to render.
Question 3Easy
Which function maps a URL pattern to a view in urls.py?
- route_to()
- url_for()
- path()
- link()
Answer: C. path()
Explanation
path('about/', views.about) routes the URL to the view.
Step-by-step solution
- Import path.
- Add to urlpatterns.
Why the other options are wrong
- A.
route_to() is not a real Django function; the correct choice is path(). - B. That is Flask.
- D.
link() is not a real Django function; the correct choice is path().
Question 4Easy
Which function in django.urls maps a URL pattern to a view?
- path()
- route()
- url_map()
- link()
Answer: A. path()
Explanation
path('articles/', views.list) is the standard form.
Step-by-step solution
- Django uses path() to define URL patterns.
- path() connects URLs to views in urls.py.
- Other options are not valid Django URL functions.
- Correct function is path().
- Final answer: A (path()).
Why the other options are wrong
- B. route() is not a Django function; URL patterns are declared with path().
- C. url_map() is not a Django function in urls; path() maps the URL to the view.
- D. link() is not a Django URL function; the mapping is written with path().
Tip: Remember, path() is the standard way to map URLs in Django.
Question 5Medium
What is the purpose of the name argument in path(..., name='detail')?
- Sets the HTML page title shown in the browser tab
- Restricts the route to one particular HTTP method
- Enables reverse lookup via reverse() or {% url %}
- Names the template file that the view will render
Answer: C. Enables reverse lookup via reverse() or {% url %}
Explanation
Named URLs avoid hardcoding paths.
Step-by-step solution
- The name argument gives a URL pattern a label.
- reverse() and the {% url %} tag use that label to build the URL.
- So the answer is option C, reverse lookup.
Why the other options are wrong
- A. The page title is set in the template, not with name in path().
- B. The HTTP method is limited in the view, not with name.
- D. The view chooses the template; name does not name a template file.
Tip: Name every path and use {% url 'name' %} instead of hardcoding URLs.
Question 6Medium
What does get_object_or_404(Post, pk=5) do if no row matches?
- Returns None so the view must check the result and decide what response to send
- Creates the object
- Raises a 500 error always
- Raises Http404 so a 404 page is shown
Answer: D. Raises Http404 so a 404 page is shown
Explanation
The shortcut wraps .get() and converts DoesNotExist into Http404.
Step-by-step solution
- Query runs.
- No match gives Http404.
Why the other options are wrong
- A. It does not return None.
- B. It never creates.
- C. It converts to 404.
Question 7Medium
Which status code does redirect('home') return by default?
- 302
- 301
- 200
- 404
Answer: A. 302
Explanation
redirect returns a temporary redirect unless permanent=True.
Step-by-step solution
- Call redirect.
- Default 302.
Why the other options are wrong
- B. Permanent=True gives 301.
- C. 200 is OK, not redirect.
- D. 404 is not found.
Question 8Medium
In path('post/<int:pk>/', view), what does <int:pk> do?
- Checks in the database that a row with that pk exists
- Makes the pk segment optional in the URL pattern
- Passes pk to the view as a query string parameter
- Captures an integer and passes it to the view as pk
Answer: D. Captures an integer and passes it to the view as pk
Explanation
A path converter captures and converts the segment.
Step-by-step solution
- Inside path(), the part in angle brackets is a converter.
- The int converter matches digits in the URL.
- The matched value is converted to an integer and passed to the view as the keyword argument pk.
- So the answer is option D.
Why the other options are wrong
- A. Converters match the URL text; they do not query the database.
- B. The converter does not make the segment optional.
- C. The value comes from the URL path, not the query string.
Tip: path('post/<int:pk>/', view) gives the view a keyword argument pk as an integer.
Question 9Hard
What is true about HttpResponseRedirect('/x/') after a successful POST?
- It repeats the POST request automatically so the form data is submitted again
- It follows the Post/Redirect/Get pattern to avoid duplicate submissions on refresh
- It clears the session
- It returns JSON
Answer: B. It follows the Post/Redirect/Get pattern to avoid duplicate submissions on refresh
Explanation
After redirect the browser issues a GET, so reloading does not resubmit the form.
Step-by-step solution
- POST handled.
- Redirect sent.
- Browser GETs.
Why the other options are wrong
- A. Browsers use GET after redirect.
- C. Sessions are unaffected.
- D. It returns a redirect.
Question 10Hard
What does this URL pattern capture?
re_path(r'^year/(?P<y>[0-9]{4})/$', views.year)
- Any number of digits, because the group has no limit on how many it accepts
- A positional argument only
- Nothing
- A four-digit value passed as the keyword argument y
Answer: D. A four-digit value passed as the keyword argument y
Explanation
Named regex groups become keyword arguments to the view.
Step-by-step solution
- Regex matches digits.
- Group named y.
Why the other options are wrong
- A. Exactly four digits.
- B. Named groups give keyword args.
- C. It captures y.
Practise all 20 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 →