Lesson 4 β URLs & Routing in Django
In this lesson, you will learn how Django finds and displays pages using URLs & routing.
By the end of todayβs lesson, you will be able to:
β Understand how project URLs and app URLs work together
β Use path() and re_path()
β Create routes with parameters like <id>
β Add URLs for your own apps (home and blog)
β Prepare for the homework: /poems/<id>/ route
This lesson uses your actual project:
hello_django/
config/
urls.py
home/
views.py
urls.py
blog/
views.py
urls.py
π 1. How Django Handles URLs
When you open your project in the browser:
http://YOUR-SERVER-IP:8011/
Django starts looking for URLs in this order:
- hello_django/config/urls.py β main router
- It includes URLs from each installed app
- Then each app handles its own routes
Think of it like this:
- config/urls.py = traffic police
- home/urls.py = routes for homepage, about page, etc.
- blog/urls.py = routes for blog posts
π 2. Project URLs (config/urls.py)
Open:
hello_django/config/urls.py
It should look like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')), # Home app
path('blog/', include('blog.urls')), # Blog app
]
This tells Django:
- When the URL starts with nothing (
"") β go to home.urls - When URL starts with
"blog/"β go to blog.urls
π 3. App URLs
Each app needs its own urls.py.
You already created them in Lesson 3 β now we add routes.
π home/urls.py
home/
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_page, name='home'),
path('about/', views.about_page, name='about'),
]
home/views.py
from django.http import HttpResponse
def home_page(request):
return HttpResponse("<h1>Welcome to Home Page</h1>")
def about_page(request):
return HttpResponse("<h1>About Page</h1>")
Now the following works:
//about/
π blog/urls.py
blog/
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.blog_index, name='blog_index'),
]
blog/views.py
from django.http import HttpResponse
def blog_index(request):
return HttpResponse("<h1>Blog Home</h1>")
This creates:
/blog/
π§ 4. Using path() and re_path()
β path() β the one you will use 99% of the time
path('post/<int:id>/', views.post_detail)
β re_path() β for advanced URLs using regex
(We rarely need this now.)
Example:
from django.urls import re_path
re_path(r'^article/(?P<slug>[-\w]+)/$', views.article_detail)
π’ 5. Route Parameters (Very Important)
Django lets you capture values from the URL.
Example:
/blog/post/5/
In the URL:
path('post/<int:id>/', views.post_detail)
In the view:
def post_detail(request, id):
return HttpResponse(f"Post ID = {id}")
π― 6. Homework
Create a route: /poems/<id>/
Inside blog/urls.py:
path('poems/<int:id>/', views.poem_detail, name='poem_detail'),
Inside blog/views.py:
def poem_detail(request, id):
return HttpResponse(f"This is poem number {id}")
Test it:
http://YOUR-IP:8011/blog/poems/7/
π 2. Lesson 4 Quiz
Here is your beginner-friendly quiz, exactly matching what you learned.
π₯ Quiz: Lesson 4 β URLs & Routing
Question 1
What is the purpose of config/urls.py in a Django project?
A. It stores all HTML templates
B. It is the main router that includes each appβs URLs
C. It stores database models
D. It contains CSS files
Question 2
Which function is used for basic routing in Django?
A. route()
B. url()
C. path()
D. redirect()
Question 3
Given this route:
path('post/<int:id>/', views.post_detail)
What URL will display the post with ID 7?
A. /post/7/
B. /post?id=7
C. /post/<7>/
D. /7/post/
Question 4
Which import is required to include URLs from an app?
A. from django.urls import link
B. from django.urls import re_path
C. from django.urls import include
D. from django.shortcuts import path
Question 5
True or False:
If you forget to create urls.py inside your app, Django will crash with a routing error.
Question 6
What does this view return?
def poem_detail(request, id):
return HttpResponse(f"This is poem number {id}")
A. Always “This is poem number 1”
B. A page showing the poem based on the URL ID
C. An error
D. An empty page
Question 7
Which URL belongs to the blog app if your config/urls.py has:
path('blog/', include('blog.urls'))
A. /blog/poems/5/
B. /poems/5/
C. /home/blog/poems/5/
D. /blog-poems/5/
βΆοΈ 7. Run Your Server on Port 8011
Use:
python manage.py runserver 0.0.0.0:8011
If everything is correct, you now have working routes:
| URL | What it shows |
|---|---|
/ | Home Page |
/about/ | About page |
/blog/ | Blog homepage |
/blog/poems/1/ | Poem #1 |
π Lesson 4 Summary
By now you understand:
β How Django routes URLs
β How project and app URLs work together
β How to pass parameters in routes
β How to build dynamic pages like /poems/7/
β How your projectβs folder structure works with routing