Last Updated : 17 May, 2025
In Django, views are Python functions that handle HTTP requests. These views process the request and return an HTTP response or an error (e.g., 404 if not found). Each view must be mapped to a specific URL pattern. This mapping is managed through URLConf (URL Configuration).
In this article, we'll explore how URL patterns are used in Django, how they are defined in urls.py, and how different URL patterns help in routing requests to appropriate views.
Understanding URLConf in DjangoIn Django, the URLConf refers to the process of mapping URLs to views. This mapping is defined in a Python module, usually called urls.py. The configuration module specified in the ROOT_URLCONF setting in settings.py determines which module is used for URL mapping.
For a project named myProject, the default ROOT_URLCONF is typically set to 'myProject.urls'. Every URLConf module contains a variable urlpatterns, which is a list or set of URL patterns that Django checks against the requested URL. The patterns are checked in sequence, and when a match is found, the corresponding view is invoked. If no match is found, Django triggers an appropriate error handling view.
Structure of URLConf in Django: Python
# myProject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('books.urls')), # Including URL patterns from the books app
]
In the above example:
URL patterns are defined inside the urlpatterns list. This list contains the routes Django uses to match incoming URLs to appropriate views.
Here is a sample urls.py for the books app:
Python
# books/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('books/<int:pk>/', views.book_detail), # Detail view for a book, identified by primary key (pk)
path('books/<str:genre>/', views.books_by_genre), # View books by genre
path('books/', views.book_index), # List all books
]
Explanation:
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request):
return render(request, 'home.html')
def about_view(request):
return render(request, 'about.html')
def contact_view(request):
return render(request, 'contact.html')
2. Create basic templates in your templates/ folder:
home.html:
HTML
<h1>Home Page</h1>
<p>Welcome to the home page.</p>
<a href="/about/">Go to About</a><br>
<a href="/contact/">Go to Contact</a>
about.html:
Python
<h1>About Page</h1>
<p>This is the about page.</p>
<a href="/">Go to Home</a><br>
<a href="/contact/">Go to Contact</a>
contact.html:
HTML
<h1>Contact Page</h1>
<p>Contact us at contact@example.com.</p>
<a href="/">Go to Home</a><br>
<a href="/about/">Go to About</a>
3. Define URL patterns in urls.py: Python
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_view, name='home'),
path('about/', views.about_view, name='about'),
path('contact/', views.contact_view, name='contact'),
]
Output:
1. Home Page:
Home Page2. Click “Go to About”:
About Page3. Click “Go to Contact”:
Contact Page Path ConvertersIn Django, path converters are used in URL patterns to capture specific values from the URL and pass them to the view as arguments. For example:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4