008-Django URLs

Objectives: Django URLs

Django URLs

Django URLs

1. App-specific URLs

Create a file named urls.py in the same folder as your views.py and add:

from django.urls import path
from . import views

urlpatterns = [
    path('members/', views.members, name='members'),
]

Real-life Example:

This is like giving directions inside a building. You tell Django “When someone goes to ‘members/’, take them to the members view.”

2. Project-level URLs

Now, route your app URLs in the root urls.py located in the project folder (my_tennis_club/my_tennis_club/urls.py):

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('members.urls')),
    path('admin/', admin.site.urls),
]

Advice:

Always use include('app_name.urls') to keep your app URLs separate. This makes your project easier to maintain as it grows.

3. Running the Server

If the server is not running, navigate to the project folder and run:

python manage.py runserver

Then open your browser and go to:

http://127.0.0.1:8000/members/

Real-life Example:

Think of the server as a waiter. Running the server makes the waiter ready to take requests. Typing the URL is like telling the waiter which table you want to serve.

Advice:

If you get a 404 error, check that your view name, app name, and paths match exactly. Typos are common at this stage.

Exercise:

What is the correct function for specifying URLs inside the urlpatterns list?

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::