← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 6 of 10

Dynamic URL के Parameters

एक URL में angle brackets, जैसे <int:id>, एक blank space हैं जो Django web address से real numbers या words से भरता है, एक fill-in-the-blank form जैसा।
Syntax
markup
urlpatterns = [
    path('route/<converter:param_name>/', views.view_function, name='url_name'),
]

def view_function(request, param_name):
    # use param_name here

एक Value Capture करना

एक URL segment को angle brackets में wrap करना, जैसे <int:post_id>, Django को बताता है कि path के उस हिस्से को capture करे और इसे view function को एक keyword argument के रूप में pass करे।

उदाहरण: Capturing a Value

Wrapping a URL segment in angle brackets, like <int:post_id>, tells Django to capture that part of the path and pass it as a keyword argument to the view function.

markup
urlpatterns = [
    path('posts/<int:post_id>/', views.post_detail, name='post_detail'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

View Argument Receive करता है

View function को URL pattern में उपयोग हुए जैसा ही एक parameter declare करना चाहिए। Django view को उस value के साथ call करता है जो already right Python type में convert हो चुकी है।

उदाहरण: The View Receives the Argument

The view function must declare a parameter with the same name used in the URL pattern. Django calls the view with that value already converted to the right Python type.

markup
from django.http import HttpResponse

def post_detail(request, post_id):
    return HttpResponse(f'Showing post #{post_id}')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Common Path Converters

Django कई built-in converters के साथ आता है: str (default, कोई slashes नहीं), int (positive integers), slug (letters, numbers, hyphens, underscores), uuid, और path (slashes भी match करता है)।

Note: SEO-friendly URLs जैसे /posts/my-first-post/ के लिए <slug:slug> उपयोग करें।

उदाहरण: Common Path Converters

Django ships several built-in converters: str (default, no slashes), int (positive integers), slug (letters, numbers, hyphens, underscores), uuid, and path (matches slashes too).

markup
urlpatterns = [
    path('posts/<slug:slug>/', views.post_by_slug),
    path('files/<path:filepath>/', views.serve_file),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. View function signature में captured parameter को argument के रूप में accept करना भूल जाना।
  2. गलत path converter उपयोग करना (जैसे एक numeric primary key के लिए <str:pk> बजाय <int:pk> के)।
  3. URL pattern और view function के argument name में parameter name match न करना।
चैप्टर सारांश
  • <int:pk> जैसे Angle-bracket segments URL का हिस्सा capture करते हैं और इसे view को argument के रूप में pass करते हैं।
  • Path converters (str, int, slug, uuid, path) captured value को अपने आप validate और convert करते हैं।
  • Brackets के अंदर का नाम view function में corresponding parameter name से exactly match होना चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.