0
0
Djangoframework~10 mins

Why views handle request logic in Django - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Django view that returns a simple HTTP response.

Django
from django.http import HttpResponse

def my_view(request):
    return [1]("Hello, world!")
Drag options to blanks, or click blank then click option'
Aredirect
Brender
CHttpResponse
DJsonResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using render without a template
Returning a redirect instead of a response
Using JsonResponse for plain text
2fill in blank
medium

Complete the code to access the HTTP method used in the request inside a Django view.

Django
def check_method(request):
    if request.[1] == 'POST':
        return HttpResponse('Posted!')
    else:
        return HttpResponse('Not a POST')
Drag options to blanks, or click blank then click option'
Abody
BGET
Cpath
Dmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.GET instead of request.method
Trying to access request.body for method
Using uppercase 'Method'
3fill in blank
hard

Fix the error in the view to correctly get a query parameter named 'name' from the request.

Django
def greet(request):
    name = request.[1].get('name', 'Guest')
    return HttpResponse(f"Hello, {name}!")
Drag options to blanks, or click blank then click option'
APOST
BGET
Cparams
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.POST for query parameters
Trying to access request.body as a dict
Using request.params which does not exist
4fill in blank
hard

Fill both blanks to create a view that renders a template with context data.

Django
from django.shortcuts import [1]

def home(request):
    context = {'title': 'Welcome'}
    return [2](request, 'home.html', context)
Drag options to blanks, or click blank then click option'
Arender
BHttpResponse
Credirect
Dget_object_or_404
Attempts:
3 left
💡 Hint
Common Mistakes
Importing render but returning HttpResponse
Using redirect instead of render
Using get_object_or_404 which is for database queries
5fill in blank
hard

Fill all three blanks to create a view that handles POST data and redirects after processing.

Django
from django.shortcuts import [1], [2]

def submit(request):
    if request.method == 'POST':
        data = request.POST.get('[3]')
        # process data here
        return redirect('success')
    return render(request, 'submit.html')
Drag options to blanks, or click blank then click option'
Arender
Bredirect
Cdata
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing redirect
Using HttpResponse instead of redirect
Using wrong POST key name