0
0
DjangoHow-ToBeginner · 4 min read

How to Use URL Parameters in Django: Simple Guide

In Django, you use path converters in your URL patterns to capture URL parameters and pass them to your view functions as arguments. You can also access query parameters from the URL using request.GET inside your views.
📐

Syntax

Django uses path() in urls.py to define URL patterns with parameters. Parameters are enclosed in angle brackets <> with an optional converter type like int or str. These parameters are passed as arguments to the view function.

Query parameters are accessed inside views via request.GET, which is a dictionary-like object.

python
from django.urls import path
from . import views

urlpatterns = [
    path('article/<int:id>/', views.article_detail, name='article_detail'),
]

# In views.py
from django.http import HttpResponse

def article_detail(request, id):
    # id is captured from URL
    page = request.GET.get('page', '1')  # query parameter with default
    return HttpResponse(f"Article ID: {id}, Page: {page}")
💻

Example

This example shows how to capture an integer id from the URL and a query parameter page from the URL query string. The view returns a simple text response showing both values.

python
# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('article/<int:id>/', views.article_detail, name='article_detail'),
]

# views.py
from django.http import HttpResponse

def article_detail(request, id):
    page = request.GET.get('page', '1')
    return HttpResponse(f"Article ID: {id}, Page: {page}")
Output
Visiting /article/42/?page=3 shows: Article ID: 42, Page: 3 Visiting /article/42/ shows: Article ID: 42, Page: 1
⚠️

Common Pitfalls

  • Forgetting to include the parameter in the URL pattern causes a TypeError in the view because the argument is missing.
  • Using the wrong converter type (e.g., int but passing a string) results in a 404 error.
  • Trying to get query parameters directly from the URL pattern instead of request.GET.
  • Not providing a default value when accessing query parameters can lead to None values.
python
# Wrong: Missing parameter in URL pattern
# urls.py
path('article/', views.article_detail),

# views.py
# def article_detail(request, id):  # id missing causes error

# Correct:
path('article/<int:id>/', views.article_detail),

def article_detail(request, id):
    page = request.GET.get('page', '1')  # safe access
📊

Quick Reference

ConceptUsageExample
Path parameterCapture part of URL as argumentpath('item/<int:id>/', view)
Query parameterAccess URL query stringrequest.GET.get('key', 'default')
ConvertersSpecify type of path parameter<int:id>, <str:name>
Default query valueAvoid None if missingrequest.GET.get('page', '1')

Key Takeaways

Use path() with angle brackets to capture URL parameters in Django URLs.
Pass captured parameters as arguments to your view functions.
Access query parameters inside views with request.GET.get() safely using defaults.
Choose the correct path converter type to avoid 404 errors.
Always provide default values when reading query parameters to prevent None issues.