我在django 2.0.7
应用程序(2.0版)的项目中使用django-decorator-include
。
我的urls.py:
from django.conf import settings
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf.urls.i18n import i18n_patterns
from django.contrib.auth.decorators import login_required
from decorator_include import decorator_include
urlpatterns = i18n_patterns(
path('', include('web.urls', namespace='web')),
path('backoffice/', decorator_include([login_required,], 'backoffice.urls', namespace='backoffice')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的backoffice.urls
:
from django.urls import path, re_path
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _
from backoffice import views
app_name = 'backoffice'
urlpatterns = (
path('', views.DashboardView.as_view(), name='dashboard'),
)
backoffice
已添加到 INSTALLED_APPS 。
在我的视图中(例如,在应用web
中),我可以执行反向URL,例如,在我的登录视图中,我可以进行return redirect('backoffice:dashboard')
,它可以正常工作-将其重定向到/ backoffice /
问题是当我尝试在模板中执行反向URL时。在我的模板之一中,当我添加代码时:
<li class="featured"><a href="{% url 'backoffice:dashboard' %}">{% trans 'Open dashboard' %}</a></li>
我收到django错误:
NoReverseMatch at /
'backoffice' is not a registered namespace
我认为问题与django-decorator-include有关,因为如果我将include更改为标准django url include,则:
path('backoffice/', include('backoffice.urls', namespace='backoffice')),
它工作得很好,我能够再次在模板中获得反向URL。 我该怎么办?也许是django-decorator-include中的错误?有什么想法可以解决吗?