0
0
Djangoframework~10 mins

Returning HTML templates 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 return an HTML template using Django's render function.

Django
from django.shortcuts import [1]

def home(request):
    return [1](request, 'home.html')
Drag options to blanks, or click blank then click option'
Arender
Bredirect
CHttpResponse
Dget_object_or_404
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render
Returning HttpResponse without rendering template
2fill in blank
medium

Complete the code to pass a context dictionary to the template.

Django
from django.shortcuts import render

def profile(request):
    context = {'name': 'Alice'}
    return render(request, 'profile.html', [1])
Drag options to blanks, or click blank then click option'
Aname
Bcontext
C'profile.html'
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the request object again
Passing the template name instead of context
3fill in blank
hard

Fix the error in the code to correctly return the template with context.

Django
from django.shortcuts import render

def dashboard(request):
    data = {'user': 'Bob'}
    return render(request, 'dashboard.html', [1]=data)
Drag options to blanks, or click blank then click option'
Adata
Brequest
Ctemplate_name
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword argument name
Passing context as positional argument but with keyword syntax
4fill in blank
hard

Fill both blanks to create a view that renders 'about.html' with a title in context.

Django
from django.shortcuts import [1]

def about(request):
    context = {'title': [2]
    return render(request, 'about.html', context)
Drag options to blanks, or click blank then click option'
Arender
B'About Us'
C'Home'
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render
Forgetting quotes around string values
5fill in blank
hard

Fill all three blanks to render 'contact.html' with name and email in context.

Django
from django.shortcuts import [1]

def contact(request):
    context = {'name': [2], 'email': [3]
    return render(request, 'contact.html', context)
Drag options to blanks, or click blank then click option'
Arender
B'John Doe'
C'john@example.com'
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render
Missing quotes around string values