Reverse url in reusable app that is consumed as a nested app

六眼飞鱼酱① 提交于 2019-12-08 04:11:30

问题


A similar question was answered here.

My situation is slightly different though. I have created a reusable app called "categories". In my project I have an app called "dashboard". The dashboard app includes the reusable "categories" app. This causes the following to be used to reverse a url

reverse('dashboard:categories:browse')

However, my reusable app has no knowledge of the "dashboard" namespace. I want to be able to use the solution I linked above to reverse only the following within the reusable categories app.

reverse('categories:browse')

Currently, setting app_name in categories.urls does not work. I get NoReverseMatch when reversing "categories:browse".

Here are excerpts of how the apps are included in the urls.py files.

# myproject/urls.py
url(
    r'^dashboard/',
    include(
        'dashboard.urls',
        namespace='dashboard',
    )
),


# dashboard/urls.py
url(
    r'^categories/',
    include(
        'categories.urls',
        namespace="categories",
    ),
),

回答1:


You can include the categories urls in your main urls.py directly:

# myproject/urls.py
url(r'^dashboard/categories/', include('categories.urls', namespace='categories')),
url(r'^dashboard/', include('dashboard.urls', namespace='dashboard')),

That way your categories urls are not in a nested namespace, and you can simply use reverse('categories:browse').



来源:https://stackoverflow.com/questions/40709613/reverse-url-in-reusable-app-that-is-consumed-as-a-nested-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!