In this article, I will explain How to Perform Routing with Django URL conf.
What is URL conf in Django?
In the first place, Django URL conf, short for URL configuration, is a key component of the Django web framework. Basically, it defines the mapping between URLs and the corresponding views that should handle the incoming HTTP requests.
In general, a URL conf is a Python module that contains a list of URL patterns, where each pattern maps a specific URL to a view. When there is a request to a Django application, the URL dispatcher goes through the URL conf and looks for a matching URL pattern. In case, there is a match, the corresponding view is called. After processing, it returns the output to the client.
Actually, the URL conf is defined in the urls.py
file. Also, it’s typically structured as a list of URL patterns and associated views. The following example demonstrates it.
from django.urls import path
from . import views
urlpatterns = [
path('blog/', views.blog_index, name='blog_index'),
path('blog/<int:post_id>/', views.blog_detail, name='blog_detail'),
]
In this example, there are two URL patterns, one for the blog index page and another for the detail page of a specific blog post. In particular, the path
function from the django.urls
module defines the URL patterns, and the views
module contains the views that will handle the incoming requests. Further, the name
argument is optional and gives each URL pattern a name. Then again, it can be used in other parts of the application, such as in templates.
Example of Routing with Django URL conf
In fact, routing with Django URL conf is performed by defining URL patterns in the urls.py
file and mapping those patterns to views. The following code demonstrates a simple example of how routing works in Django.
- Define URL patterns. The first step is to define the URL patterns in the
urls.py
file. Each pattern is defined using thepath
function from thedjango.urls
module. For example.
from django.urls import path
from . import views
urlpatterns = [
path('blog/', views.blog_index, name='blog_index'),
path('blog/<int:post_id>/', views.blog_detail, name='blog_detail'),
]
In this example, there are two URL patterns, one for the blog index page and another for the detail page of a specific blog post.
- Map URL patterns to views. The next step is to map each URL pattern to a view. A view is a Python function that takes an HTTP request as input and returns an HTTP response. For example.
from django.shortcuts import render
def blog_index(request):
return render(request, 'blog_index.html')
def blog_detail(request, post_id):
return render(request, 'blog_detail.html', {'post_id': post_id})
In this example, the blog_index
view returns the content of the blog_index.html
template, and the blog_detail
view returns the content of the blog_detail.html
template, along with a context variable post_id
.
- Handle incoming requests. Finally, when a request is made to a Django application, the URL dispatcher goes through the URL conf and looks for a matching URL pattern. If a match is found, the corresponding view is called, and the output is returned to the client.
In this way, Django URL conf provides a flexible and powerful way to perform routing in a Django application, allowing you to map URL patterns to views and handle incoming HTTP requests.
Further Reading
Introduction to Django Framework and its Features
Creating a Simple App in Django
Bringing Life to your Web Pages with Django Views
Django models: Elevating your web application to the next level