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.
How Django processes a request (URL → View → Template)
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.
home_view.urlpatterns = [
path('home/', views.home_view),
]home.html template back to the user.def home_view(request): return render(request, 'home.html')
<h1>Welcome to {{ site_name }}</h1>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!".
# 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>
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.
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.
Practice
Solution
Step 1: Understand URL routing in Django
Django uses URL patterns defined in urls.py to find which view should handle the request.Step 2: Identify the first action on a request
The first action is matching the URL to a view, not rendering or database access.Final Answer:
Django matches the URL to a view using URL patterns. -> Option BQuick Check:
URL routing = matching URL to view [OK]
- Thinking template renders before view runs
- Assuming database is accessed before URL matching
- Confusing client-side JavaScript with Django server process
urls.py?Solution
Step 1: Recall Django URL pattern syntax
Django 3+ usespath()to define URL patterns, with URL string, view function, and optional name.Step 2: Identify correct function name
url()is deprecated, androute()orlink()are invalid.Final Answer:
path('home/', views.home_view, name='home') -> Option AQuick Check:
Use path() for URL patterns [OK]
- Using deprecated url() instead of path()
- Using non-existent functions like route() or link()
- Omitting the view function argument
{{ message }}?
def hello_view(request):
context = {'message': 'Hello, Django!'}
return render(request, 'hello.html', context)Solution
Step 1: Understand context passing in Django views
The view passes a dictionary with key 'message' and value 'Hello, Django!' to the template.Step 2: Template renders variable from context
The template uses{{ message }}to display the value 'Hello, Django!'.Final Answer:
Hello, Django! -> Option AQuick Check:
Template variable = context value [OK]
- Confusing template name with output
- Thinking context dictionary prints as string
- Assuming request object is shown in template
def my_view(request):
return render('my_template.html')Solution
Step 1: Check render function signature
render() requires at least two arguments: request and template name.Step 2: Identify missing argument
The code only passes the template name, missing the request argument.Final Answer:
The render function is missing the request argument. -> Option DQuick Check:
render(request, template) needs request first [OK]
- Forgetting to pass request to render()
- Thinking template name should be a dict
- Confusing render with redirect functions
/products/. The URL pattern matches products_view. The view fetches products from the database and passes them to products.html template. The template shows the product names in a list.
Which is the correct order of steps Django follows internally?Solution
Step 1: URL matching
Django first matches the incoming URL to the correct view function using URL patterns.Step 2: View processes request
The view fetches data (products) from the database and prepares context for the template.Step 3: Template rendering and response
The template uses the context to render HTML, which Django sends as the HTTP response.Final Answer:
URL -> View fetches data -> Template renders with data -> Response sent -> Option CQuick Check:
URL then view then template then response [OK]
- Thinking template renders before URL matching
- Mixing order of view and URL matching
- Assuming response sent before processing
Ready to prove it?
Pass the test and this pattern moves unknown
