Webmaster and More

Smart WordPress & AI-Powered Solutions for Your Business

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:

  1. hello_django/config/urls.py β†’ main router
  2. It includes URLs from each installed app
  3. Then each app handles its own routes

Think of it like this:


πŸ“ 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:


πŸ“ 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:


πŸ“ 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:

URLWhat it shows
/Home Page
/about/About page
/blog/Blog homepage
/blog/poems/1/Poem #1

Homework and quiz solutions


πŸŽ‰ 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