问题
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