0
0
Djangoframework~10 mins

Function-based views basics in Django - Interactive Code Practice

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

Complete the code to import the function to create HTTP responses in Django.

Django
from django.http import [1]
Drag options to blanks, or click blank then click option'
AHttpRequest
BHttpResponse
Crender
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing HttpRequest instead of HttpResponse
Using render instead of HttpResponse for simple responses
2fill in blank
medium

Complete the function-based view to return a simple HTTP response.

Django
def home_view(request):
    return [1]("Hello, world!")
Drag options to blanks, or click blank then click option'
Arender
BJsonResponse
CHttpResponse
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using render without a template
Returning a string directly instead of an HttpResponse
3fill in blank
hard

Fix the error in the view function to correctly accept the request parameter.

Django
def about_view([1]):
    return HttpResponse("About page")
Drag options to blanks, or click blank then click option'
Acontext
Bself
Cresponse
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' which is for class methods
Using 'response' or 'context' which are not request objects
4fill in blank
hard

Fill both blanks to import and use the shortcut to render a template in a function-based view.

Django
from django.shortcuts import [1]

def contact_view(request):
    return [2](request, 'contact.html')
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 directly
Using redirect instead of render for templates
5fill in blank
hard

Fill all three blanks to create a function-based view that renders a template with context data.

Django
from django.shortcuts import [1]

def profile_view(request):
    context = {'name': [2]
    return [3](request, 'profile.html', context)
Drag options to blanks, or click blank then click option'
Arender
B'Alice'
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing context to render
Using HttpResponse instead of render for templates with context