Home > Software engineering >  How to use multi-level {%url%} with Django in HTML template - Class Based Views
How to use multi-level {%url%} with Django in HTML template - Class Based Views

Time:05-25

Im utilizing class based views: I have an app that has 1 urls file in app, 2 sublevels urls files and 1 view file from my last sublevel urls file

portfolio_menu.urls:

app_name = 'portfolio'
    urlpatterns = [
        path('test', include('store.urls', namespace='store')),
        path('', views.menu),
    ]

store.urls:

app_name = 'store
urlpatterns = [
    path('', include('product.urls', namespace='product')),
]

product.urls:

app_name = 'product'
urlpatterns = [
    path('', views.ProductListView.as_view(), name='list'),
]

the funcion I want to get, but don't know the sintax:

<a href="{%url portfolio:store:product:list%}"></a> #I wish something like that

CodePudding user response:

Use only one of these: namespace or app_name. It should be clearer.

app_name = 'portfolio'
urlpatterns = [
    path('test', include('store.urls')),
    ...
]


app_name = 'store'
urlpatterns = [
    path('', include('product.urls')),
]


app_name = 'product'
urlpatterns = [
    path('', views.ProductListView.as_view(), name='list'),
]

In template it should work exactly as you wanted, but use it as a string with: ''.

<a href="{% url 'portfolio:store:product:list' %}">
  • Related