In this lesson, you will learn how to create views and templates in Django to render dynamic web pages. Views are the backbone of your web application, handling the logic for what data to display, and templates define how to present that data to the user.
A view in Django is a Python function or class that receives a web request and returns a web response. This response can be HTML content, a redirect, a 404 error, a JSON response, or essentially any other form of output.
Let's start with a simple function-based view:
# views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, World!")
In this example, when a user visits the URL mapped to this view, they will see "Hello, World!" displayed in their browser.
Django also supports class-based views, which provide a more object-oriented approach:
# views.py
from django.views import View
from django.http import HttpResponse
class HomeView(View):
def get(self, request):
return HttpResponse("Hello, World from Class-Based View!")
To connect your views to URLs, you need to modify your urls.py file. Here’s how you can map the above views:
# urls.py
from django.urls import path
from .views import home, HomeView
urlpatterns = [
path('', home, name='home'),
path('class/', HomeView.as_view(), name='home_class'),
]
Templates in Django are used to define the layout and design of your HTML pages. Django uses its own templating language to allow you to embed dynamic content within HTML.
First, create a directory called templates within your app directory. Inside it, create a file named home.html:
<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>This is a dynamic page rendered using Django templates.</p>
</body>
</html>
Now, modify your view to render this template:
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
You can also pass data from your views to your templates. Here’s how:
# views.py
def home(request):
context = {'message': 'Hello, World!'}
return render(request, 'home.html', context)
Then, in your home.html template, you can access the message variable:
<!-- templates/home.html -->
<body>
<h1>{{ message }}</h1>
<p>This is a dynamic page rendered using Django templates.</p>
</body>
Note: Always ensure your templates are located in the correct directory path as specified in your Django settings.
Common Mistake: Forgetting to include the
renderfunction fromdjango.shortcutswhen rendering templates.
In this lesson, you learned how to create views and templates in Django to render dynamic web pages. You explored both function-based and class-based views, how to map them to URLs, and how to use templates to present data.
Create a new view that returns a greeting message. Map this view to a new URL and test it in your browser.
Create a new template named about.html that displays information about your website. Render this template in a new view.
Modify your about.html template to accept a dynamic variable (like a user's name) from the view and display it on the page.