Python | Django Views | Function Based Views

Last Updated :
Discuss
Comments

Question 1

What does a Django view function receive as its main argument to handle an incoming request, and what type of object must it return?

  • A string path and it returns a template name

  • An HttpRequest instance and it returns an HttpResponse or a subclass

  • A model queryset and it returns a JSON dictionary

  • A URL pattern and it returns a redirect path

Question 2

Which module provides HttpResponse for returning a basic response from a view, and what is the general purpose of render() in Django?

  • from django.views import HttpResponse and render adds styling

  • from django.urls import HttpResponse and render fixes errors

  • from django.http import HttpResponse and render helps build a complete HTTP response

  • from django.db import HttpResponse and render runs database operations

Question 3

Which attribute on the request object is used to read query parameters, and how does this differ from accessing POST data?

  • request.GET and POST is only for files

  • request.GET and POST is used for submitted form data while both act like dictionary objects

  • request.query and POST is more secure

  • request.POST and GET is used for file handling

Question 4

In a view that handles creating a new item, what is the usual behavior when checking request.method == 'POST'?

  • POST means process and save the submitted data, and GET means show the input form

  • The view treats every request as POST

  • GET is used to save data and POST shows the form

  • The method check is not needed because both methods act the same

Question 5

When form.is_valid() returns False in a create view, what happens to the submitted data and how are the validation errors shown again?

  • The data is saved anyway and errors only appear in logs

  • An exception is raised and nothing is shown back to the user

  • The data is checked and if invalid, the same form is returned so the errors can be displayed

  • The invalid data is ignored and a partial save happens

Question 6

When using Model.objects.filter(active=True) in a view, what does this return, and why might an order_by() call be added afterward?

  • A single object and ordering changes it randomly

  • A saved record and ordering applies only after saving

  • A URL pattern and ordering always defaults to ascending

  • A queryset of matching records and ordering arranges the results in a desired sequence

Question 7

To paginate a list with 10 items per page, which import and code correctly set up the Paginator and handle page number defaults?

  • paginator = Paginator(queryset, 10) and page_obj = paginator.page(request.GET.get('page', 1))

  • Use slice on the queryset and skip imports

  • Import from django.views and read the page from POST

  • Call paginator.page(10) with a default page of 0

Question 8

In a detail view that retrieves an object by its ID, which approach safely returns the object or a 404 error, and why is it better than using filter().first()?

  • get_object_or_404(Model, pk=pk) because it returns a proper 404 when no object is found

  • Model.objects.all().get(pk=pk) because it always raises errors

  • Model.objects.filter(pk=pk).first() because it silently returns None

  • A raw SQL query because it is always the safest

Question 9

In a detail view after retrieving an object with get_object_or_404, which option correctly returns a response that includes the object in the context?

  • HttpResponse(object.title) which does not include context data

  • Creating a form with the object which does not build a response context

  • redirect() which does not return the object in the response

  • render(request, 'page.html', {'object': object}) which returns a response with context

Question 10

In an update view after retrieving an object with get_object_or_404, which option correctly initializes a form with that object so it can be edited?

  • form = MyForm(request.POST, instance=obj) if POST else MyForm(instance=obj)

  • Always create a new blank form and apply the instance only during save

  • form = MyForm(obj) which does not properly set the instance

  • Use a create flow and replace the object by deleting and adding a new one

Tags:

There are 15 questions to complete.

Take a part in the ongoing discussion