0
0
Djangoframework~20 mins

Why views handle request logic in Django - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Views Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why do Django views handle request logic?

In Django, views are responsible for processing incoming HTTP requests. Why is it important that views handle this request logic?

ABecause views store the database models and handle data migrations automatically.
BBecause views manage the server configuration and deployment settings.
CBecause views are used to style the HTML pages with CSS and JavaScript.
DBecause views connect the URL requested by the user to the code that processes data and returns a response.
Attempts:
2 left
💡 Hint

Think about what happens when a user visits a web page and how Django decides what to show.

component_behavior
intermediate
1:30remaining
What happens when a Django view receives a request?

Consider a Django view function that receives a GET request. What is the typical behavior of this view?

Django
from django.http import HttpResponse, HttpResponseNotAllowed

def example_view(request):
    if request.method == 'GET':
        return HttpResponse('Hello, world!')
    else:
        return HttpResponseNotAllowed(['GET'])
AIt redirects the user to another website without processing the request.
BIt automatically updates the database with new data from the request.
CIt checks the request method, processes the GET request, and returns a response with content 'Hello, world!'.
DIt ignores the request method and always returns an error.
Attempts:
2 left
💡 Hint

Look at how the view checks the request method and what it returns.

state_output
advanced
1:30remaining
What is the output of this Django view when receiving a POST request?

Given the following Django view, what will be the HTTP response content if the request method is POST?

Django
from django.http import HttpResponse, HttpResponseNotAllowed

def my_view(request):
    if request.method == 'GET':
        return HttpResponse('GET request received')
    elif request.method == 'POST':
        return HttpResponse('POST request received')
    else:
        return HttpResponseNotAllowed(['GET', 'POST'])
A'POST request received'
BEmpty response with status 200
CHttpResponseNotAllowed error
D'GET request received'
Attempts:
2 left
💡 Hint

Check the condition that matches the POST method.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this Django view function

Which option contains a syntax error in the Django view function below?

Django
from django.http import HttpResponse

def sample_view(request):
    if request.method == 'GET':
        return HttpResponse('Hello')
    else:
        return HttpResponse('Other method')
AMissing colon after the if statement condition.
BHttpResponse is not imported.
CIndentation error in the else block.
DUsing single quotes instead of double quotes in strings.
Attempts:
2 left
💡 Hint

Look carefully at the if statement syntax.

🔧 Debug
expert
2:00remaining
Why does this Django view raise an error when handling a request?

Examine the Django view below. When a request is made, it raises an error. What is the cause?

Django
from django.http import HttpResponse

def error_view(request):
    if request.method == 'POST':
        data = request.POST['username']
    return HttpResponse(f'User: {data}')
AThe view does not handle GET requests, causing a 404 error.
BVariable 'data' is used outside the if block without guarantee it is assigned.
CHttpResponse is not imported, causing a NameError.
Drequest.POST does not contain 'username' key, causing a KeyError.
Attempts:
2 left
💡 Hint

Consider what happens if the request method is not POST.