0
0
Djangoframework~20 mins

Returning HTML templates in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django view return?
Consider this Django view function. What will the user see when visiting this view?
Django
from django.shortcuts import render

def home(request):
    context = {'name': 'Alice'}
    return render(request, 'home.html', context)
AA JSON response containing {'name': 'Alice'}.
BThe HTML content of 'home.html' with 'Alice' displayed where {{ name }} is used.
CA plain text response showing the string "{'name': 'Alice'}".
DAn error because 'render' is missing the request argument.
Attempts:
2 left
💡 Hint
Remember that render combines a template with context data to produce HTML.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Django view
What is wrong with this Django view code?
Django
from django.shortcuts import render

def about(request):
    context = {'page': 'About'}
    return render('about.html', context)
AThe render function is missing the request argument as the first parameter.
BThe function should return HttpResponse, not render.
CThe template name should be a variable, not a string.
DThe context dictionary should be a list, not a dict.
Attempts:
2 left
💡 Hint
Check the order and number of arguments for render.
state_output
advanced
2:00remaining
What is the output HTML of this Django view?
Given this template and view, what HTML will be rendered?
Django
Template 'greet.html':
<html><body><h1>Hello, {{ user }}!</h1></body></html>

View:
from django.shortcuts import render

def greet(request):
    return render(request, 'greet.html', {'user': 'Bob'})
A<html><body><h1>Hello, Bob!</h1></body></html>
B<html><body><h1>Hello, {{ user }}!</h1></body></html>
C<html><body><h1>Hello, !</h1></body></html>
DA server error because 'user' is not defined in the view.
Attempts:
2 left
💡 Hint
Variables in templates are replaced by context values.
🔧 Debug
advanced
2:00remaining
Why does this Django view raise TemplateDoesNotExist?
This view raises a TemplateDoesNotExist error. What is the most likely cause?
Django
from django.shortcuts import render

def contact(request):
    return render(request, 'contact_us.html')
AThe render function requires a context dictionary, which is missing.
BThe render function should return a redirect, not a template.
CThe template file 'contact_us.html' is missing or not in the template search path.
DThe view function is missing the request argument.
Attempts:
2 left
💡 Hint
Check if the template file exists in your templates folder.
🧠 Conceptual
expert
2:00remaining
What happens if you return render() without a context?
In Django, what is the behavior when you call render(request, 'index.html') without providing a context dictionary?
AIt causes a server error because the template expects variables not provided.
BIt raises a TypeError because the context argument is required.
CIt returns a JSON response with empty data.
DThe template renders normally with no variables replaced, as context defaults to an empty dictionary.
Attempts:
2 left
💡 Hint
Check the default value of the context parameter in render.