App-Level URLs को Include करना
# 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.
# 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.
# 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.
# 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. #}
- Namespaced reverse() lookups उपयोग करते समय हर app के urls.py को app_name देना भूल जाना।
- एक app की urls.py को trailing prefix slash के बिना include करना, जो unexpected concatenated paths produce करता है।
- वही 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 करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: