path() के साथ URL Patterns
path('route/', views.view_function, name='url_name')
# Example: path('about/', views.about, name='about')
path() की Anatomy
django.urls.path() एक route string (बिना leading slash), एक view function, और प्रोजेक्ट में इस URL को कहीं और refer करने के लिए उपयोग होने वाला एक optional name keyword लेता है।
उदाहरण: Anatomy of path()
django.urls.path() takes a route string (without a leading slash), a view function, and an optional name keyword used to refer back to this URL elsewhere in the project.
from django.urls import path
from . import views
urlpatterns = [
path('about/', views.about, name='about'),
]
{# 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. #}
Multiple Static Routes
एक urlpatterns list usually कई path() entries रखती है, app में हर static route के लिए एक। हर एक एक अलग URL string को अपने खुद के view function से map करती है।
उदाहरण: Multiple Static Routes
A urlpatterns list usually holds several path() entries, one per static route in the app. Each maps a distinct URL string to its own view function.
urlpatterns = [
path('', views.home, name='home'),
path('contact/', views.contact, name='contact'),
path('pricing/', views.pricing, name='pricing'),
]
{# 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. #}
Reverse Lookups के लिए URLs को Name देना
path() को दिया गया name= argument आपको string को hardcode किए बिना उस URL को कहीं और refer करने देता है, Django के {% url %} template tag या reverse() function का उपयोग करके। अगर URL path कभी बदले, तो सिर्फ path() call को update करना होता है।
उदाहरण: Naming URLs for Reverse Lookups
The name= argument given to path() lets you refer to that URL elsewhere without hardcoding the string, using Django's {% url %} template tag or the reverse() function. If the URL path ever changes, only the path() call needs updating.
from django.urls import reverse
# Anywhere in Python code:
url = reverse('about')
# In a template: <a href="{% url 'about' %}">About</a>
{# 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. #}
- path() string में trailing slash भूल जाना, जो site में inconsistent URLs का कारण बनता है।
- path() को एक view call pass करना, जैसे views.home(), बजाय function reference views.home के।
- name= argument छोड़ना, जो {% url %} या reverse() से बाद में URL reference करना मुश्किल बनाता है।
- path() एक URL pattern string, एक view function, और reverse lookups के लिए एक optional name लेता है।
- View को हमेशा एक reference के रूप में pass करें (views.home), कभी call न करें (views.home())।
- name= के साथ हर URL को name देना templates और redirects को URL changes के लिए resilient बनाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: