Challenge - 5 Problems
TemplateView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django TemplateView render?
Given the following Django TemplateView, what will be the rendered output when accessing the URL mapped to this view?
Django
from django.views.generic import TemplateView class WelcomeView(TemplateView): template_name = "welcome.html" # Assume welcome.html contains: <h1>Welcome to our site!</h1>
Attempts:
2 left
💡 Hint
Check what the template_name attribute points to and what the template contains.
✗ Incorrect
The TemplateView renders the template specified by template_name. Since welcome.html contains the heading, that is what will be rendered.
❓ state_output
intermediate2:00remaining
What context data is available in this TemplateView?
Consider this Django TemplateView:
from django.views.generic import TemplateView
class InfoView(TemplateView):
template_name = "info.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = 'Information Page'
return context
What is the value of 'page_title' in the template context when rendering info.html?
Attempts:
2 left
💡 Hint
Look at how get_context_data adds 'page_title' to the context dictionary.
✗ Incorrect
The get_context_data method adds 'page_title' with value 'Information Page' to the context, so the template can access it.
📝 Syntax
advanced2:00remaining
Which option correctly overrides TemplateView to pass extra context?
You want to add extra context data 'user_role' with value 'admin' to a TemplateView. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Remember to call super() when overriding get_context_data to preserve existing context.
✗ Incorrect
Option D correctly overrides get_context_data, calls super(), adds 'user_role', and returns the updated context.
🔧 Debug
advanced2:00remaining
Why does this TemplateView raise TemplateDoesNotExist?
Given this view:
from django.views.generic import TemplateView
class AboutView(TemplateView):
template_name = 'about_us.html'
But when accessing the URL, Django raises TemplateDoesNotExist: about_us.html.
What is the most likely cause?
Attempts:
2 left
💡 Hint
Check if the template file exists in the correct folder.
✗ Incorrect
TemplateDoesNotExist means Django cannot find the template file. The most common cause is the file missing or in the wrong folder.
🧠 Conceptual
expert2:00remaining
How does TemplateView handle HTTP GET requests internally?
Which statement best describes how Django's TemplateView processes an HTTP GET request?
Attempts:
2 left
💡 Hint
Think about the flow of data from request to response in TemplateView.
✗ Incorrect
TemplateView's get() method calls get_context_data(), renders the template with that context, and returns the response automatically.