0
0
Djangoframework~20 mins

TemplateView for simple pages in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TemplateView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
A<h1>Hello, user!</h1>
BTemplateDoesNotExist error
C<h1>Welcome to our site!</h1>
DEmpty page with no content
Attempts:
2 left
💡 Hint
Check what the template_name attribute points to and what the template contains.
state_output
intermediate
2: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?
ANone
BInformation Page
Cpage_title
DRaises AttributeError
Attempts:
2 left
💡 Hint
Look at how get_context_data adds 'page_title' to the context dictionary.
📝 Syntax
advanced
2: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?
A
class MyView(TemplateView):
    template_name = 'dashboard.html'
    extra_context = {'user_role': 'admin'}
B
class MyView(TemplateView):
    template_name = 'dashboard.html'
    def get_context_data(self):
        return {'user_role': 'admin'}
C
class MyView(TemplateView):
    template_name = 'dashboard.html'
    def get_context(self):
        return {'user_role': 'admin'}
D
class MyView(TemplateView):
    template_name = 'dashboard.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['user_role'] = 'admin'
        return context
Attempts:
2 left
💡 Hint
Remember to call super() when overriding get_context_data to preserve existing context.
🔧 Debug
advanced
2: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?
AThe template file about_us.html is missing from the templates directory.
BThe template_name attribute is misspelled and should be templateName.
CTemplateView requires a render() method to be defined.
DThe URL pattern is not mapped to AboutView.
Attempts:
2 left
💡 Hint
Check if the template file exists in the correct folder.
🧠 Conceptual
expert
2:00remaining
How does TemplateView handle HTTP GET requests internally?
Which statement best describes how Django's TemplateView processes an HTTP GET request?
ATemplateView calls get_context_data(), renders the template with context, and returns an HttpResponse.
BTemplateView uses a form to process GET data before rendering.
CTemplateView requires a manually defined get() method to handle GET requests.
DTemplateView directly returns the template file content without context processing.
Attempts:
2 left
💡 Hint
Think about the flow of data from request to response in TemplateView.