Lesson 15 of 60 – Django URL Parameters
25%

Django URL Parameters

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/.

Note: Django provides URL converters such as int, str, slug, uuid, and path for capturing values from URLs.

1. What is a URL Parameter?

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.

2. Why Use URL Parameters?

URL parameters are useful when we want one view to handle different values.

  • Student details
  • Product details
  • Blog posts
  • Categories
  • User profiles
  • Order details

3. Basic URL Parameter

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.

4. URL Parameter in a View

def student(request, id):
    return HttpResponse(f"Student ID: {id}")

The id parameter from the URL becomes the id argument of the view.

5. Complete Basic Example

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}")

6. Integer URL Converter

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/

7. Integer Parameter in View

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.

8. String URL Converter

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/

9. String Parameter in View

def user(request, name):
    return HttpResponse(f"Hello {name}")

If the URL is /user/rahul/, the value rahul is passed to the view.

10. Slug Converter

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

11. Slug Parameter in View

def blog(request, title):
    return HttpResponse(f"Blog: {title}")

A slug can be used to identify a particular article or page.

12. Multiple URL Parameters

A URL can contain more than one parameter.

path(
    'student/<int:id>/<str:name>/',
    views.student
)

For example:

/student/10/rahul/

13. View with Multiple Parameters

def student(request, id, name):
    return HttpResponse(
        f"ID: {id}, Name: {name}"
    )

The parameter names in the URL pattern should match the view arguments.

14. URL Parameter and Database Records

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}
    )

15. Product Details Example

path(
    'product/<int:id>/',
    views.product
)

A URL such as /product/15/ can display product number 15.

16. Category Parameter

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}")

17. UUID Converter

Django also provides a uuid converter for UUID values.

path(
    'order/<uuid:id>/',
    views.order
)

This can be useful when records use UUID identifiers.

18. Path Converter

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.

19. Available Common Converters

  • str – matches a non-empty string
  • int – matches positive integers
  • slug – matches slug-like strings
  • uuid – matches UUID values
  • path – matches a string including path separators

20. Parameter Names

You 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)

21. URL Parameters and Query Strings

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.

22. Accessing Query Parameters

Query parameters are accessed using the request object's GET attribute.

def students(request):
    class_name = request.GET.get('class')

    return HttpResponse(class_name)

23. URL Parameter with Template

def student(request, id):
    return render(
        request,
        'student.html',
        {'student_id': id}
    )

The template can display the value:

<h1>Student ID: {{ student_id }}</h1>

24. URL Parameter Example for Blog

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.

25. URL Parameter Example for Employee

path(
    'employee/<int:id>/',
    views.employee
)
def employee(request, id):
    return HttpResponse(
        f"Employee ID: {id}"
    )

26. URL Parameter Validation

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.

27. Common Mistake: Parameter Name Mismatch

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.

28. Complete URL Parameter Example

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/

29. Practical URL Parameter Example

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.

30. Summary of Django URL Parameters

URL parameters make Django URLs dynamic and allow values to be passed directly to views.

  • URL parameters are written inside < >.
  • 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.
  • Multiple parameters can be used in one URL.
  • Parameters are passed to the view as arguments.
  • URL parameters are useful for dynamic pages.

📌 Key Points

  • URL parameters allow dynamic values in Django URLs.
  • Use <int:id> when an integer is expected.
  • Use <str:name> for string values.
  • Use <slug:slug> for URL-friendly text.
  • Use multiple parameters when required.
  • The URL parameter is passed to the corresponding view.
  • URL parameters are different from query string parameters.
  • They are commonly used for detail pages and database records.

🧠 Quick Quiz

Question: Which URL converter is used to capture an integer value?