In this lesson, you will learn how URL routing works in Django. URL configuration tells Django what should happen when a user visits a particular URL.
urls.py.
URL stands for Uniform Resource Locator. It is the address used to access a resource on the web.
For example:
http://127.0.0.1:8000/about/
Here, /about/ identifies a particular page or resource.
URL routing is the process of matching a requested URL with the appropriate Django view.
Django checks the URL patterns and determines which view should handle the request.
Django normally creates a urls.py file when you create a
project.
myproject/
urls.py
This file contains the main URL configuration for the project.
Django uses a variable called urlpatterns to store URL patterns.
urlpatterns = [
]
URL patterns are added inside this list.
Django provides the path() function for defining URL patterns.
from django.urls import path
A URL pattern commonly uses path() to connect a URL to a view.
Suppose a view called home exists. A URL pattern can be written as:
path('', home)
This pattern can be used for the project's root URL.
Before using a view in urls.py, it must be available to the
URL configuration.
For example:
from . import views
Then a view can be referenced as views.home.
A simple URL configuration can look like this:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
]
Here, the empty path represents the root URL.
The root URL is the main URL of the website.
For example:
http://127.0.0.1:8000/
It can be mapped using an empty path:
path('', views.home)
You can create a URL for an About page:
path('about/', views.about)
The corresponding browser URL would be:
http://127.0.0.1:8000/about/
A Django project can contain many URL patterns.
urlpatterns = [
path('', views.home),
path('about/', views.about),
path('contact/', views.contact),
]
Each URL can be connected to a different view.
A URL pattern connects a URL to a view.
| URL | View |
|---|---|
/ |
views.home |
/about/ |
views.about |
/contact/ |
views.contact |
Django URL patterns commonly use a trailing slash.
path('about/', views.about)
This pattern corresponds to the URL ending with /about/.
URL patterns can be given names using the name argument.
path('about/', views.about, name='about')
Naming URLs makes it easier to refer to them elsewhere in a Django project.
Suppose a URL is named about:
path('about/', views.about, name='about')
The name can later be used with Django's URL reversing features instead of hard-coding the URL in templates or Python code.
Django can capture values from URLs using path converters.
For example:
path('student/<int:id>/', views.student)
Here, id is captured from the URL and passed to the view.
The int converter matches an integer value.
path('student/<int:id>/', views.student)
A URL such as /student/10/ can match this pattern.
The str converter can capture a non-empty string that does
not include a slash.
path('profile/<str:username>/', views.profile)
A URL such as /profile/rahul/ can match this pattern.
The slug converter can match ASCII letters or numbers,
hyphens and underscores.
path('post/<slug:title>/', views.post)
It is useful for URL-friendly values such as article identifiers.
A complete example can look like this:
path('student/<int:id>/', views.student, name='student')
The captured id value is passed to the associated view.
Larger Django projects commonly keep application URLs in the app itself. The project's main URL configuration can include them.
from django.urls import include, path
urlpatterns = [
path('students/', include('students.urls')),
]
The include() function helps organize URL patterns into
separate application-level URL configuration files.
This is especially useful when a project contains multiple applications.
A Django app can have its own urls.py file.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
The main project URL configuration can include these patterns.
A common structure is:
myproject/
urls.py
students/
urls.py
The project's urls.py can include the URL patterns from
the students app.
A new Django project normally includes a URL for the Django admin site.
path('admin/', admin.site.urls)
This maps the /admin/ URL to Django's admin site.
Django checks URL patterns in the order in which they appear in
urlpatterns.
Therefore, URL patterns should be organized carefully, especially when patterns could potentially match similar URLs.
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
]
This example connects three application pages and the Django admin site.
After changing URL patterns, run the development server and visit the corresponding URL in your browser.
python manage.py runserver
Then test the URL in the browser.
pathCheck the terminal error message carefully when a URL does not work.
Django URL routing connects browser requests with views.
URL patterns are stored in urlpatterns and commonly use
the path() function.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
For larger projects, include() can be used to organize
application URLs into separate files.
urls.py contains Django URL configuration.urlpatterns stores URL patterns.path() is commonly used to define URL patterns.name argument.int, str and slug.include() helps organize URLs for separate Django apps.Question: Which function is commonly used to define URL patterns in Django?