django urls and views
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Tutorial 06 - Django URLs and Views
Why this matters
One of the most important Django concepts is how a browser URL becomes Python code.
The core relationship is:
URL
↓
View
↓
Response1. A simple view
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello!")When Django calls home, it gives the function a request object.
The function returns a response.
2. Connecting a URL
In urls.py:
from django.urls import path
from .views import home
urlpatterns = [
path("", home),
]Now:
Browser requests /
↓
Django matches ""
↓
home()3. Multiple pages
urlpatterns = [
path("", home),
path("about/", about),
path("contact/", contact),
]Now:
/ → home
/about/ → about
/contact/ → contact4. Rendering a template
Instead of returning plain text:
return HttpResponse("Hello")use:
from django.shortcuts import render
def home(request):
return render(request, "main/home.html")The flow becomes:
URL
↓
home()
↓
home.html
↓
HTML response5. Passing data
def home(request):
name = "CookieSensei"
return render(
request,
"main/home.html",
{"name": name},
)Template:
<h1>Welcome to {{ name }}</h1>The dictionary is context.
6. Querying data
products = Product.objects.all()
return render(
request,
"main/products.html",
{"products": products},
)The template can loop through them:
{% for product in products %}
<h2>{{ product.name }}</h2>
{% endfor %}7. Dynamic URLs
You can capture information from the URL:
path(
"products/<int:id>/",
product_detail,
)For:
/products/7/Django can call:
def product_detail(request, id):
...with:
id = 78. Getting an object
You may see:
product = Product.objects.get(id=id)If it does not exist, this can raise an exception.
A common Django pattern is:
from django.shortcuts import get_object_or_404
product = get_object_or_404(
Product,
id=id,
)This produces a 404 response when the record cannot be found.
9. Redirects
After an action:
return redirect("home")can send the browser to another route.
A common flow:
Submit form
↓
Save
↓
Redirect
↓
Confirmation/list page10. Debugging 404 errors
If /products/5/ gives a 404, trace:
Browser URL
↓
root urls.py
↓
included app urls.py
↓
path()
↓
viewDo not immediately change the view if Django is not reaching it.
11. Exercise
Create:
/
about/
/contact/
/products/
/products/1/Make /products/ display a list and /products/1/ display one product.
12. Using ChatGPT
Ask:
Here are myurls.pyandviews.py./products/5/gives a 404. Explain how Django matches this URL and identify the likely problem. Do not rewrite the entire project.
Remember
URL
↓
URL pattern
↓
View
↓
Logic
↓
ResponseA Django page is usually not a file sitting at that URL. The URL is routed to Python code that decides what response to produce.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →