0
0
Djangoframework~30 mins

TemplateView for simple pages in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Add a URL pattern with path 'about/' and use TemplateView.as_view with template_name='about.html'.