URL parameters with angle brackets let you capture parts of a web address as variables. This helps your website show different content based on the URL.
0
0
URL parameters with angle brackets in Django
Introduction
You want to show a user profile page based on the user ID in the URL.
You need to display a blog post using its unique slug from the URL.
You want to filter products by category name passed in the URL.
You want to pass a date or number in the URL to show specific data.
You want to create clean, readable URLs that include dynamic parts.
Syntax
Django
path('route/<converter:name>/', view_function, name='route_name')
The angle brackets <> capture URL parts as variables.
The converter defines the type of variable (like int, str, slug).
Examples
This captures an integer from the URL as
user_id.Django
path('user/<int:user_id>/', views.user_detail, name='user_detail')
This captures a slug (letters, numbers, hyphens) as
post_slug.Django
path('post/<slug:post_slug>/', views.post_detail, name='post_detail')
This captures any string as
category_name.Django
path('category/<str:category_name>/', views.category_view, name='category_view')
Sample Program
This example shows a URL pattern that captures a username from the URL and passes it to the greet_user view. The view then returns a greeting message using that username.
Django
from django.urls import path from django.http import HttpResponse # View function that uses URL parameter def greet_user(request, username): return HttpResponse(f"Hello, {username}!") # URL patterns list urlpatterns = [ path('hello/<str:username>/', greet_user, name='greet_user'), ]
OutputSuccess
Important Notes
Use the correct converter to match the expected data type.
If you omit the converter, Django treats the parameter as a string by default.
Angle brackets must be used exactly as shown to capture parameters.
Summary
Angle brackets <> in Django URLs capture parts of the URL as variables.
Converters like int, str, and slug define the variable type.
This helps create dynamic, readable URLs that pass data to views.