URL parameters allow us to pass dynamic values from a URL to a Django view.
For example, instead of creating a separate URL for every student, we can use
one URL pattern such as student/10/ or student/25/.
int,
str, slug, uuid, and path for
capturing values from URLs.
A URL parameter is a dynamic value included in a URL and passed to a Django view.
student/10/
Here, 10 can be used as a student ID.
URL parameters are useful when we want one view to handle different values.
A basic URL parameter can be created using angle brackets.
path('student/<id>/', views.student)
The value captured by <id> is passed to the view.
def student(request, id):
return HttpResponse(f"Student ID: {id}")
The id parameter from the URL becomes the id argument
of the view.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('student/<id>/', views.student),
]
views.py
from django.http import HttpResponse
def student(request, id):
return HttpResponse(f"Student ID: {id}")
The int converter is used when the URL parameter should contain an integer.
path('student/<int:id>/', views.student)
For example:
/student/10/
/student/25/
/student/100/
def student(request, id):
return HttpResponse(f"Student ID is {id}")
If the URL is /student/25/, the value 25 is passed to
the id parameter.
The str converter captures a non-empty string that does not include
the path separator /.
path('user/<str:name>/', views.user)
Example:
/user/rahul/
/user/amit/
def user(request, name):
return HttpResponse(f"Hello {name}")
If the URL is /user/rahul/, the value rahul is passed
to the view.
The slug converter is useful for URL-friendly text.
path('blog/<slug:title>/', views.blog)
Examples of slug values include:
django-tutorial
python-basics
web-development
def blog(request, title):
return HttpResponse(f"Blog: {title}")
A slug can be used to identify a particular article or page.
A URL can contain more than one parameter.
path(
'student/<int:id>/<str:name>/',
views.student
)
For example:
/student/10/rahul/
def student(request, id, name):
return HttpResponse(
f"ID: {id}, Name: {name}"
)
The parameter names in the URL pattern should match the view arguments.
URL parameters are commonly used to retrieve a particular database record.
def student(request, id):
student = Student.objects.get(id=id)
return render(
request,
'student.html',
{'student': student}
)
path(
'product/<int:id>/',
views.product
)
A URL such as /product/15/ can display product number 15.
path(
'category/<str:name>/',
views.category
)
The category name can be passed directly to the view.
def category(request, name):
return HttpResponse(f"Category: {name}")
Django also provides a uuid converter for UUID values.
path(
'order/<uuid:id>/',
views.order
)
This can be useful when records use UUID identifiers.
The path converter captures a string that can contain path separators.
path(
'files/<path:file_path>/',
views.files
)
It can capture values such as nested paths.
str – matches a non-empty stringint – matches positive integersslug – matches slug-like stringsuuid – matches UUID valuespath – matches a string including path separatorsYou can choose meaningful names for URL parameters.
path(
'student/<int:student_id>/',
views.student
)
The view should use the same parameter name:
def student(request, student_id):
return HttpResponse(student_id)
A URL path parameter and a query string are different concepts.
/student/10/
Here 10 is part of the URL path.
/students/?class=10
Here class=10 is a query string parameter.
Query parameters are accessed using the request object's GET attribute.
def students(request):
class_name = request.GET.get('class')
return HttpResponse(class_name)
def student(request, id):
return render(
request,
'student.html',
{'student_id': id}
)
The template can display the value:
<h1>Student ID: {{ student_id }}</h1>
path(
'blog/<slug:slug>/',
views.blog_detail
)
def blog_detail(request, slug):
return HttpResponse(
f"Showing blog: {slug}"
)
A URL such as /blog/django-basics/ can use this pattern.
path(
'employee/<int:id>/',
views.employee
)
def employee(request, id):
return HttpResponse(
f"Employee ID: {id}"
)
Django's built-in converters help restrict the type of value accepted by a URL pattern.
path(
'student/<int:id>/',
views.student
)
The int converter expects an integer value in that position.
The parameter name in the URL pattern should match the view argument.
URL:
path('student/<int:id>/', views.student)
View:
def student(request, id):
return HttpResponse(id)
Using a different argument name can cause an error when Django calls the view.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path(
'student/<int:id>/',
views.student
),
]
views.py
from django.http import HttpResponse
def student(request, id):
return HttpResponse(
f"Student ID: {id}"
)
Visit:
http://127.0.0.1:8000/student/10/
Suppose a website needs to display different products using their IDs. Instead of creating separate URL patterns, one dynamic pattern can be used.
path(
'product/<int:id>/',
views.product
)
def product(request, id):
return HttpResponse(
f"Product ID: {id}"
)
The same view can handle /product/1/,
/product/2/, and other valid integer IDs.
URL parameters make Django URLs dynamic and allow values to be passed directly to views.
< >.int is used for integer values.str is used for strings.slug is useful for URL-friendly text.uuid is used for UUID values.path can capture path separators.<int:id> when an integer is expected.<str:name> for string values.<slug:slug> for URL-friendly text.Question: Which URL converter is used to capture an integer value?