Reputation: 1513
Supposed my views.py
contain multiple views that are meant to go deeper in the url path.
urlpatterns = [
path('<int:table_pk>/order/', views.menu_category_view, name='menu_category_view'),
path('<str:menu_category>/', views.menu_item_view, name='menu_item_view'),
]
The first view has the path generated as <int:table_pk>/order/
.
If I want the second view to have a path as following <int:table_pk>/order/<str:menu_category>/
, how should i go about passing the variables (in the view and/or template) in DRY manner?
What if I have more path levels that need to extend the path above it in this same manner?
Upvotes: 1
Views: 226
Reputation: 476493
You can work with an include(…)
clause [Django-doc]:
from django.urls import include
urlpatterns = [
path('<int:table_pk>/order/', include([
path('', views.menu_category_view, name='menu_category_view'),
path('<str:menu_category>/', views.menu_item_view, name='menu_item_view'),
]))
]
The path(…)
before the include(…)
is thus common for both path(…)
s in the list wrapped in the include(…)
.
You can thus construct a hierarchy by using include
s where a path(…)
can be wrapped in an include(…)
of another path(…)
, etc.
Upvotes: 3