Views & URLs Questions with Answers

20 practice questions on Views & URLs (Django) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

Which shortcut renders a template and returns an HttpResponse?

  1. display(request)
  2. html(request, 'page.html'), a shortcut that returns the raw file as a response
  3. render(request, 'page.html', context)
  4. 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

  1. Provide request, template, context.
  2. 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?

  1. response
  2. request
  3. self
  4. context

Answer: B. request

Explanation

Django passes an HttpRequest object to the view.

Step-by-step solution

  1. URL matched.
  2. 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?

  1. route_to()
  2. url_for()
  3. path()
  4. link()

Answer: C. path()

Explanation

path('about/', views.about) routes the URL to the view.

Step-by-step solution

  1. Import path.
  2. 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?

  1. path()
  2. route()
  3. url_map()
  4. link()

Answer: A. path()

Explanation

path('articles/', views.list) is the standard form.

Step-by-step solution

  1. Django uses path() to define URL patterns.
  2. path() connects URLs to views in urls.py.
  3. Other options are not valid Django URL functions.
  4. Correct function is path().
  5. 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')?

  1. Sets the HTML page title shown in the browser tab
  2. Restricts the route to one particular HTTP method
  3. Enables reverse lookup via reverse() or {% url %}
  4. 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

  1. The name argument gives a URL pattern a label.
  2. reverse() and the {% url %} tag use that label to build the URL.
  3. 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?

  1. Returns None so the view must check the result and decide what response to send
  2. Creates the object
  3. Raises a 500 error always
  4. 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

  1. Query runs.
  2. 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?

  1. 302
  2. 301
  3. 200
  4. 404

Answer: A. 302

Explanation

redirect returns a temporary redirect unless permanent=True.

Step-by-step solution

  1. Call redirect.
  2. 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?

  1. Checks in the database that a row with that pk exists
  2. Makes the pk segment optional in the URL pattern
  3. Passes pk to the view as a query string parameter
  4. 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

  1. Inside path(), the part in angle brackets is a converter.
  2. The int converter matches digits in the URL.
  3. The matched value is converted to an integer and passed to the view as the keyword argument pk.
  4. 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?

  1. It repeats the POST request automatically so the form data is submitted again
  2. It follows the Post/Redirect/Get pattern to avoid duplicate submissions on refresh
  3. It clears the session
  4. 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

  1. POST handled.
  2. Redirect sent.
  3. 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)
  1. Any number of digits, because the group has no limit on how many it accepts
  2. A positional argument only
  3. Nothing
  4. 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

  1. Regex matches digits.
  2. 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 →

Related topics

See all Django topics · All topics