Dynamic URL के Parameters
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.
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.
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 करता है)।
उदाहरण: 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).
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. #}
- View function signature में captured parameter को argument के रूप में accept करना भूल जाना।
- गलत path converter उपयोग करना (जैसे एक numeric primary key के लिए <str:pk> बजाय <int:pk> के)।
- 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 होना चाहिए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: