URL parameter type converters help Django understand what kind of data to expect in a URL. This makes your web app handle URLs more safely and clearly.
URL parameter type converters in Django
path('route/<converter:name>/', view_function, name='route_name')
The converter tells Django what type to expect (like int, str, slug, uuid, or path).
The name is the variable name your view will receive.
id from the URL and passes it to the view.path('article/<int:id>/', views.article_detail, name='article_detail')
username.path('user/<slug:username>/', views.user_profile, name='user_profile')
file_path.path('files/<path:file_path>/', views.show_file, name='show_file')
order_id.path('order/<uuid:order_id>/', views.order_detail, name='order_detail')
This Django URL pattern uses an int converter to get an article ID from the URL. The view shows the ID in the response.
from django.urls import path from django.http import HttpResponse def show_article(request, id): return HttpResponse(f"Article ID is {id}") urlpatterns = [ path('article/<int:id>/', show_article, name='show_article'), ]
If the URL part does not match the converter type, Django returns a 404 error automatically.
You can create custom converters if needed by defining a class with regex, to_python(), and to_url() methods.
The str converter matches any text except slashes, so it is the default if no converter is given.
URL parameter type converters tell Django what kind of data to expect in URLs.
They help validate and convert URL parts automatically before your view runs.
Common converters include int, str, slug, uuid, and path.
Practice
int converter do in a Django URL pattern?Solution
Step 1: Understand the role of converters
Converters tell Django what type of data to expect and convert URL parts accordingly.Step 2: Identify the behavior of
Theintconverterintconverter matches only digits and converts them to Python integers.Final Answer:
It matches only integer values and converts them to Python int type. -> Option BQuick Check:
int converter = matches integers [OK]
- Thinking int matches strings with letters
- Confusing int with slug or str converters
- Assuming int matches UUIDs
post_slug in a Django URL pattern?Solution
Step 1: Identify the correct converter for slugs
Theslugconverter matches letters, numbers, underscores, and hyphens.Step 2: Match syntax with converter and parameter name
The syntax is<slug:post_slug>inside the path string.Final Answer:
path('post/<slug:post_slug>/', views.post_detail) -> Option AQuick Check:
slug converter syntax = <slug:name> [OK]
- Using int converter for slug parameters
- Mixing UUID or path converters incorrectly
- Missing colon between converter and parameter name
path('user//', views.profile) and the URL /user/42/, what will be the type and value of user_id in the view?Solution
Step 1: Analyze the URL pattern converter
Theintconverter converts the URL part to an integer.Step 2: Check the URL segment matched
The URL segment '42' matches theintconverter and becomes integer 42.Final Answer:
An integer 42 -> Option DQuick Check:
int converter converts to int type [OK]
- Assuming user_id is a string
- Confusing int with uuid or path converters
- Thinking the value includes slashes
path('article//', views.detail) when the URL /article/abc/ is requested.Solution
Step 1: Understand the int converter behavior
Theintconverter only matches digits; 'abc' is not digits.Step 2: Check URL matching result
Since 'abc' does not matchint, Django returns a 404 error (no match).Final Answer:
URL does not match because 'abc' is not an integer, causing a 404 error. -> Option CQuick Check:
int converter rejects non-digit URL parts [OK]
- Thinking view gets string 'abc' anyway
- Expecting automatic type conversion errors in view
- Confusing 404 with server error
Solution
Step 1: Identify the need to match slashes in URL parameter
Regularstrconverter stops at slashes, so it cannot capture paths with slashes.Step 2: Choose the converter that allows slashes
Thepathconverter matches any string including slashes, perfect for file paths.Final Answer:
Usepathconverter because it matches any string including slashes. -> Option AQuick Check:
path converter = matches strings with slashes [OK]
- Using str converter which stops at slashes
- Confusing slug or uuid converters for paths
- Expecting int converter to match strings
Ready to prove it?
Pass the test and this pattern moves unknown
