Django URLconf डिज़ाइन: नाम वाले routes से views जोड़ें
Django का URLconf URL paths को views से जोड़ता है। Django patterns को क्रम से जाँचता है और पहले मेल खाने वाले को चलाता है। स्पष्ट path converters और नाम वाले URLs routing को पढ़ना तथा बदलना सुरक्षित बनाते हैं। भूमिकाएँ अलग रखें: URLconf path और view जोड़ता है; view response और application work संभालता है।
Django का URLconf URL paths को views से जोड़ता है। Django patterns को क्रम से जाँचता है और पहले मेल खाने वाले को चलाता है। स्पष्ट path converters और नाम वाले URLs routing को पढ़ना तथा बदलना सुरक्षित बनाते हैं। भूमिकाएँ अलग रखें: URLconf path और view जोड़ता है; view response और application work संभालता है।
अनुरोध view तक कैसे पहुँचता है
डिफ़ॉल्ट रूप से Django, ROOT_URLCONF में बताए module में urlpatterns खोजता है। वह patterns को ऊपर से नीचे जाँचता है, अनुरोधित path से पहले मेल को चुनता है, और view को HttpRequest तथा captured path arguments देता है। कोई pattern न मिले या प्रक्रिया में exception आए तो Django error-handling view चलाता है।
मान लें project URLconf, polls.urls से polls/ include करता है और app URLconf path('int:question_id/', views.detail, name='detail') घोषित करता है। /polls/34/ अनुरोध detail को question_id=34 देकर बुलाता है। int:... numeric path segment लेता है और integer देता है। path() pattern के आगे slash न लगाएँ। URLconf path से मेल करता है, domain या query string से नहीं।
क्रम व्यवहार बदलता है। विशेष path को व्यापक pattern से पहले रखें यदि उसे पहले match होना चाहिए। अनुरोध गलत view तक पहुँचे तो देखें कि पहले का व्यापक pattern उसे पकड़ तो नहीं रहा। अंतिम slash भी जाँचें: अंतिम slash माँगने वाला pattern /articles/2003 से मेल नहीं खा सकता।
ऐप सीमाएँ और converters चुनें
फ़ीचर बढ़ने पर routes को app URLconfs में बाँटें और include() से जोड़ें। Django पहले से मिले parent prefix को हटाकर बाकी path को included URLconf में खोजता है। project polls/ include करे तो polls.urls उसके नीचे खाली path और int:question_id/ तय कर सकता है।
बिल्ट-इन converters में int, str, slug, uuid और path हैं। Converter तय करता है कि कौन-सा text match होगा और view को कौन-सा Python value मिलेगा: int integer, uuid UUID object देता है। slash वाला मान चाहिए तो path लें और view में कई segments संभालें। अधिक सीमित converter खराब addresses जल्दी अस्वीकार करता है।
URL को नाम देने से template और code में path string दोहराना नहीं पड़ता। pattern को name='detail' दें, फिर template में {% url 'polls:detail' question.id %} या Python में reverse('polls:detail', args=[question.id]) से URL बनाएँ। path बदले पर नाम और arguments का अनुबंध वही रहे तो कम links बदलने होंगे। ऐप में नाम टकरा सकते हैं; app_name और namespace का उपयोग करें।
function या class-based view चुनें
Function-based view input और output को एक जगह दिखाता है; छोटा flow, साफ branch या खास response के लिए यह उपयोगी है। Class-based view inheritance या mixin से व्यवहार दोहराता है; Django generic views list और detail जैसे सामान्य ढाँचे देते हैं। एक template के लिए TemplateView चुनें; class view को URLconf में as_view() से जोड़ें।
Class-based view अपने आप छोटा या बेहतर design नहीं होता। अगर बदलाव के लिए कई inheritance layers और mixin पढ़ने पड़ें तो function view अधिक स्पष्ट हो सकता है। कई screens में समान ढाँचा हो तो generic view देखें। HTTP method के अनुसार व्यवहार अलग करना भी class view का लाभ हो सकता है।
छोटे URLconf से जोड़ाव जाँचें
मान लें app URLconf list और detail routes परिभाषित करता है:
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
]Project URLconf path('polls/', include('polls.urls')) से app जोड़ता है। detail view का parameter नाम question_id होना चाहिए और links में namespaced name उपयोग करें। यह दस्तावेज़ के routing नियम का उदाहरण है; यह दावा नहीं कि project files बनाए या चलाए गए।
routing समस्या दोहराएँ और जाँचें
पुष्टि करें कि project settings का ROOT_URLCONF सही module बताता है।
देखें कि project path और app urlpatterns सही include() से जुड़े हैं।
pattern क्रम और पहले pattern द्वारा request capture होने की जाँच करें।
path values तथा converters देखें; <int:id> में अंक होने चाहिए।
पुष्टि करें कि view HttpResponse (या subclass) लौटाता है अथवा Http404 जैसा समर्थित exception उठाता है।
template या Python में path string जोड़ने के बजाय namespaced URL names लें।
development server पर अपेक्षित arguments सहित path माँगें और installed Django version के docs से मिलाएँ।
मेल न मिले तो pattern क्रम, included prefix, converter और view parameter names को एक-एक करके तुलना करें। फिर ROOT_URLCONF और middleware द्वारा request.urlconf बदलने की जाँच करें। सुधार के बाद पुष्टि करें कि direct request तथा reverse() या template URL generation उसी pattern पर पहुँचते हैं।
स्रोत
[Django 6.0: URL dispatcher](https://docs.djangoproject.com/en/6.0/topics/http/urls/)
[Django 6.0: Writing your first Django app, part 3](https://docs.djangoproject.com/en/6.0/intro/tutorial03/)
[Django 6.0: Class-based views](https://docs.djangoproject.com/en/6.0/topics/class-based-views/)
[Django 6.0: Writing views](https://docs.djangoproject.com/en/6.0/topics/http/views/)
[Django 6.0: Shortcut functions](https://docs.djangoproject.com/en/6.0/topics/http/shortcuts/)