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

App-Level URLs को Include करना

include() main map को एक पूरे section को एक छोटे, app-specific map को सौंपने देता है, ताकि हर app अपनी street का ध्यान रखे बजाय पूरे city के लिए एक giant map के।
Syntax
markup
# project/urls.py
from django.urls import path, include

urlpatterns = [
    path('prefix/', include('app_name.urls')),
]

URLs को हर App के हिसाब से Split करना

हर app और project के लिए एक root urls.py में हर route list करने के बजाय, हर app अपना खुद का urls.py define करता है, और project की root urls.py include() उपयोग करके एक URL prefix इसे delegate करती है।

उदाहरण: Splitting URLs Per App

Instead of listing every route for every app in one root urls.py, each app defines its own urls.py, and the project's root urls.py uses include() to delegate a URL prefix to it.

markup
# myproject/urls.py
from django.urls import path, include

urlpatterns = [
    path('blog/', include('blog.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. #}

App का अपना urls.py

App के अंदर, urls.py exactly एक root URLconf जैसे urlpatterns define करता है, सिवाय इसके कि इसके paths ऊपर include() call में उपयोग हुए prefix के relative हैं।

उदाहरण: The App's Own urls.py

Inside the app, urls.py defines urlpatterns exactly like a root URLconf, except its paths are relative to the prefix used in the include() call above.

markup
# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('<int:pk>/', views.post_detail, name='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. #}

app_name से Namespacing

App के urls.py में app_name set करना आपको इसके routes को कहीं और unambiguously reference करने देता है, जैसे reverse('blog:post_detail'), भले ही किसी दूसरे app का भी post_detail नाम वाला URL हो।

उदाहरण: Namespacing with app_name

Setting app_name in the app's urls.py lets you reference its routes unambiguously elsewhere, e.g. reverse('blog:post_detail'), even if another app also has a URL named post_detail.

markup
# blog/urls.py
app_name = 'blog'

urlpatterns = [
    path('<int:pk>/', views.post_detail, name='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. Namespaced reverse() lookups उपयोग करते समय हर app के urls.py को app_name देना भूल जाना।
  2. एक app की urls.py को trailing prefix slash के बिना include करना, जो unexpected concatenated paths produce करता है।
  3. वही route prefix root urls.py और app की urls.py दोनों में duplicate करना।
चैप्टर सारांश
  • include() root URLconf को एक पूरा URL prefix एक app के अपने urls.py को delegate करने देता है।
  • हर app अपने routes को self-contained रखता है, जो project URLconf को छोटा और organized रखता है।
  • app_name के साथ include() में एक namespace namespaced {% url %} lookups जैसे 'blog:post_detail' enable करता है।

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.