Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
TemplateView for simple pages
📖 Scenario: You are building a simple website with a homepage and an about page. You want to use Django's TemplateView to serve these pages without writing extra view logic.
🎯 Goal: Create two simple pages using Django's TemplateView: a homepage and an about page. Each page should display a heading with its name.
📋 What You'll Learn
Create a Django URL pattern for the homepage using TemplateView
Create a Django URL pattern for the about page using TemplateView
Use the correct template names: home.html and about.html
Each template should have a heading with the page name
💡 Why This Matters
🌍 Real World
Many websites have simple pages like home and about that do not need complex logic. Using TemplateView lets you serve these pages quickly and cleanly.
💼 Career
Understanding TemplateView is useful for Django developers to build maintainable websites with minimal code for static pages.
Progress0 / 4 steps
1
Create the homepage template
Create a file named home.html inside your Django templates folder. Add an <h1> heading with the text Home Page.
Django
Hint
Use a simple HTML file with an <h1> tag containing 'Home Page'.
2
Create the about page template
Create a file named about.html inside your Django templates folder. Add an <h1> heading with the text About Page.
Django
Hint
Make another simple HTML file with an <h1> tag containing 'About Page'.
3
Add URL pattern for homepage using TemplateView
In your Django app's urls.py, import TemplateView from django.views.generic. Add a URL pattern for path '' that uses TemplateView.as_view(template_name='home.html') and name it 'home'.
Django
Hint
Use path with an empty string for the homepage URL and TemplateView.as_view with template_name='home.html'.
4
Add URL pattern for about page using TemplateView
In the same urls.py, add a URL pattern for path 'about/' that uses TemplateView.as_view(template_name='about.html') and name it 'about'.
Django
Hint
Add a URL pattern with path 'about/' and use TemplateView.as_view with template_name='about.html'.
Practice
(1/5)
1. What is the main purpose of Django's TemplateView?
easy
A. To manage user authentication and sessions
B. To handle form submissions and validations
C. To connect to the database and fetch records
D. To display a simple static page using a specified template
Solution
Step 1: Understand TemplateView's role
TemplateView is designed to render a template without extra logic, ideal for static pages.
Step 2: Compare with other views
Other views like form views or model views handle forms or data, but TemplateView just shows a template.
Final Answer:
To display a simple static page using a specified template -> Option D
Quick Check:
TemplateView = static page display [OK]
Hint: TemplateView shows templates only, no data or forms [OK]
Common Mistakes:
Confusing TemplateView with form or data views
Thinking TemplateView handles database queries
Assuming TemplateView manages user sessions
2. Which is the correct way to specify the template file in a Django TemplateView?
easy
A. template_name = 'home.html'
B. templateFile = 'home.html'
C. template = 'home.html'
D. templateFileName = 'home.html'
Solution
Step 1: Recall TemplateView attribute
The attribute to set the template file is template_name.
Step 2: Check syntax correctness
Options A, B, and D use incorrect attribute names not recognized by Django.
Final Answer:
template_name = 'home.html' -> Option A
Quick Check:
Use template_name to set template [OK]
Hint: Always use template_name to set the template file [OK]
Common Mistakes:
Using template instead of template_name
Using camelCase instead of snake_case
Misspelling the attribute name
3. Given this Django view code, what will be the rendered output when visiting the URL?
from django.views.generic import TemplateView
class AboutPageView(TemplateView):
template_name = 'about.html'
Assuming about.html contains <h1>About Us</h1>, what will the browser show?
medium
A. A blank page with no content
B. An error because no context is provided
C. <h1>About Us</h1> displayed in the browser
D. The raw HTML code <h1>About Us</h1> as plain text
Solution
Step 1: Understand TemplateView behavior
TemplateView renders the specified template as HTML without needing extra context.
Step 2: Check template content
The template about.html contains <h1>About Us</h1>, so this will be rendered as a heading.
Final Answer:
<h1>About Us</h1> displayed in the browser -> Option C
Quick Check:
TemplateView renders template content as HTML [OK]
Hint: TemplateView shows template HTML as rendered page [OK]
Common Mistakes:
Expecting an error without context
Thinking raw HTML code shows as text
Assuming TemplateView needs extra code to render
4. What is wrong with this Django TemplateView code?
from django.views.generic import TemplateView
class ContactView(TemplateView):
template = 'contact.html'
medium
A. The attribute should be template_name, not template
B. The template file must be a .txt file, not .html
C. The template file must be in a templates folder named 'contact'
D. The class must inherit from View, not TemplateView
Solution
Step 1: Check attribute name for template
The correct attribute to specify the template file in TemplateView is template_name.
Step 2: Identify the error in code
This code uses template, which Django does not recognize, causing the view to fail to find the template.
Final Answer:
The attribute should be template_name, not template -> Option A
Quick Check:
Use template_name attribute for templates [OK]
Hint: Use template_name, not template, to specify template file [OK]
Common Mistakes:
Using template instead of template_name
Assuming template folder name must match view name
Thinking template files must be .txt
5. You want to create a simple Terms and Conditions page using Django's TemplateView. Which of these is the best way to do it?
hard
A. from django.views.generic import TemplateView
class TermsView(TemplateView):
template_name = 'terms.html'
def get(self, request):
return HttpResponse('Terms page')
B. from django.views.generic import TemplateView
class TermsView(TemplateView):
template_name = 'terms.html'
# In urls.py
path('terms/', TermsView.as_view(), name='terms')
C. from django.views import View
class TermsView(View):
def get(self, request):
return render(request, 'terms.html')
D. from django.shortcuts import render
def terms_view(request):
return render(request, 'terms.html')
Solution
Step 1: Use TemplateView for simple static pages
TemplateView is designed to serve static templates easily by setting template_name.
Step 2: Check URL configuration
from django.views.generic import TemplateView
class TermsView(TemplateView):
template_name = 'terms.html'
# In urls.py
path('terms/', TermsView.as_view(), name='terms') correctly uses as_view() and sets the URL path, which is the standard pattern.
Step 3: Compare other options
from django.views.generic import TemplateView
class TermsView(TemplateView):
template_name = 'terms.html'
def get(self, request):
return HttpResponse('Terms page') overrides get incorrectly and returns plain HttpResponse, losing template rendering. from django.views import View
class TermsView(View):
def get(self, request):
return render(request, 'terms.html') uses View but misses render import and is more complex. from django.shortcuts import render
def terms_view(request):
return render(request, 'terms.html') uses a function view, which works but is not using TemplateView.
Final Answer:
Use TemplateView with template_name and as_view() in urls.py -> Option B