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

URL Configuration की मूल बातें

URLconf Django का map है — यह website को बताता है कौन सा page दिखाना है जब कोई browser में एक certain address type करता है।
Syntax
markup
from django.urls import path
from . import views

urlpatterns = [
    path('route/', views.view_function, name='url_name'),
]

एक URLconf क्या है

एक URLconf एक Python module है जिसमें urlpatterns नाम की एक list होती है। हर entry एक URL pattern (एक string के रूप में) को एक view function से map करती है जिसे उससे match करने वाली requests handle करनी चाहिए।

उदाहरण: What a URLconf Is

A URLconf is a Python module containing a list named urlpatterns. Each entry maps a URL pattern (as a string) to a view function that should handle requests matching it.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    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. #}

ROOT_URLCONF Setting

settings.py में एक ROOT_URLCONF value होती है जो Django को बताती है कौन सा module top-level urlpatterns list रखता है। यह usually वह urls.py है जो startproject द्वारा आपके project folder में generate होती है।

उदाहरण: ROOT_URLCONF Setting

settings.py has a ROOT_URLCONF value telling Django which module contains the top-level urlpatterns list. This is usually the urls.py generated in your project folder by startproject.

markup
ROOT_URLCONF = 'myproject.urls'
{# 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. #}

Matching Order मायने रखता है

Django urlpatterns को top से bottom तक walk करता है और पहले pattern पर रुकता है जो requested path से match करे। बहुत जल्दी रखा गया एक broad pattern गलती से उन requests को swallow कर सकता है जो इसके नीचे किसी ज़्यादा specific pattern के लिए meant थीं।

Note: ज़्यादा specific paths को ज़्यादा general वालों के ऊपर रखें।

उदाहरण: Matching Order Matters

Django walks urlpatterns from top to bottom and stops at the first pattern that matches the requested path. A broad pattern placed too early can accidentally swallow requests meant for a more specific one below it.

markup
urlpatterns = [
    path('blog/latest/', views.latest_post),
    path('blog/<int:pk>/', views.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. #}
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() entry के बाद trailing comma भूल जाना, जो urls.py में एक Python syntax error का कारण बनता है।
  2. View functions लिखना जो urls.py असल में import करता है उससे match नहीं करते, जो एक ImportError का कारण बनता है।
  3. यह मान लेना कि URL order मायने नहीं रखता — Django patterns को top to bottom match करता है और पहले match पर रुकता है।
चैप्टर सारांश
  • urlpatterns path() entries की एक Python list है जो URL strings को view functions से map करती है।
  • Django urlpatterns को top-to-bottom जांचता है और request path से match करने वाला पहला pattern उपयोग करता है।
  • Root URLconf settings.py में ROOT_URLCONF setting से point किया जाता है।

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.