URL Configuration की मूल बातें
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.
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.
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 थीं।
उदाहरण: 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.
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. #}
- path() entry के बाद trailing comma भूल जाना, जो urls.py में एक Python syntax error का कारण बनता है।
- View functions लिखना जो urls.py असल में import करता है उससे match नहीं करते, जो एक ImportError का कारण बनता है।
- यह मान लेना कि 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 किया जाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: