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

path() के साथ URL Patterns

path() एक address label लिखने जैसा है — यह Django को exactly बताता है कौन सा web address कौन से page की ओर ले जाना चाहिए।
Syntax
markup
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.

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

markup
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 करना होता है।

Note: हमेशा अपने URLs को name दें, भले ही आपको लगे आपको कभी इन्हें lookup नहीं करना पड़ेगा।

उदाहरण: 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.

markup
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. #}
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. path() string में trailing slash भूल जाना, जो site में inconsistent URLs का कारण बनता है।
  2. path() को एक view call pass करना, जैसे views.home(), बजाय function reference views.home के।
  3. 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 बनाता है।

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.