Django helps turn a web address into a web page by following clear steps. It finds the right code to run and shows the right page to the user.
0
0
How Django processes a request (URL → View → Template)
Introduction
When a user types a website address and you want to show them a page.
When you want to organize your website so each URL shows different content.
When you want to separate how you get data (view) from how you show it (template).
When you want to add new pages to your website easily by linking URLs to code.
When you want to reuse page layouts with templates for consistent design.
Syntax
Django
urlpatterns = [
path('some-url/', views.some_view, name='some_name'),
]
# views.py
from django.shortcuts import render
def some_view(request):
context = {'key': 'value'}
return render(request, 'template.html', context)
# template.html
<html>
<body>
<p>{{ key }}</p>
</body>
</html>URL pattern: Connects a web address to a view function.
View function: Handles the request and sends data to the template.
Examples
This connects the URL '/home/' to the view function
home_view.Django
urlpatterns = [
path('home/', views.home_view),
]The view sends the
home.html template back to the user.Django
def home_view(request): return render(request, 'home.html')
This template shows a heading using a variable passed from the view.
Django
<h1>Welcome to {{ site_name }}</h1>Sample Program
This example shows how Django takes the URL '/greet/', runs greet_view, and sends the greet.html template with the name 'Friend'. The page will say "Hello, Friend!".
Django
# urls.py from django.urls import path from . import views urlpatterns = [ path('greet/', views.greet_view, name='greet'), ] # views.py from django.shortcuts import render def greet_view(request): context = {'name': 'Friend'} return render(request, 'greet.html', context) # greet.html <html lang="en"> <head> <meta charset="UTF-8"> <title>Greeting</title> </head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>
OutputSuccess
Important Notes
Django matches URLs in order, so put specific URLs before general ones.
Views can send data to templates using a context dictionary.
Templates use double curly braces {{ }} to show data from views.
Summary
Django turns a URL into a view function using URL patterns.
The view prepares data and chooses a template to show.
The template displays the final page using data from the view.