这个“问题”与:django 2.0 url.py include namespace="xyz"
密切相关之前的开发使用了Django 1.9(或者甚至可能在之前),但我们现在正在迁移到Django 2.0。 我们在同一个项目中有3个站点,所有站点只有1个特定的URLS Conf
### MAIN PROJECT REFERENCING ALL SITES ###
### Nothing changed here ###
from django.conf import settings
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path
from frontend import urls as frontend_urls
from search import urls as search_urls
from international import urls as international_urls
# Customisation admin
admin.site.site_header = 'INT - ADMIN'
temppatterns = [
# Admin Sites
url(r'^admin/', admin.site.urls),
# Organisation Sites
url(r'^frontend/', include(frontend_urls)),
# 1st Platform
url(r'^search/', include(search_urls)),
# 2nd Platform
url(r'^international/', include(international_urls)),
]
urlpatterns = temppatterns + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
这是前面的前端网址CONF
FRONTEND_PATTERNS = [
url(r'^conseiller$', views.GuidanceView.as_view(), name='guidance'),
.......
url(r'^contact$', views.ContactView.as_view(), name='contact'),
url(r'^$', views.HomeView.as_view(), name='home'),
]
COMPANY_PATTERNS = [
url(r'^companydetail/(?P<slug>[-\w]+)/$',
views.MemberLgView.as_view(),
name='lg-detail'),
url(r'^asso/orga/(?P<slug>[-\w]+)/$',
views.MemberOrgView.as_view(),
name='org-detail'),
]
CONTENT_PATTERNS = [
.......
]
EVENT_PATTERNS = [
.......
]
REDIRECT_PATTERNS = [
url(r'^actualite/(?P<pk>\d+)/(?P<slug>[-\w]+)/$',
views.OldBlogRedirectView.as_view(),
name='blog-redirect'),
.......
url(r'^ressources$',
RedirectView.as_view(
url=reverse_lazy('frontend:doc'), permanent=True)),
]
urlpatterns = [
url(r'^', include(FRONTEND_PATTERNS, namespace='frontend')),
url(r'^', include(COMPANY_PATTERNS, namespace='companies')),
url(r'^', include(CONTENT_PATTERNS, namespace='contents')),
url(r'^', include(REDIRECT_PATTERNS, namespace='redirects')),
url(r'^', include(EVENT_PATTERNS, namespace='events')),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但是现在,包含命名空间似乎已被弃用。所以我们在这个文档中改变了一些东西
# Added app_name
app_name="frontend"
# Deleting namespace - deprecated
urlpatterns = [
re_path(r'^', include(FRONTEND_PATTERNS)),
re_path(r'^', include(COMPANY_PATTERNS)),
re_path(r'^', include(CONTENT_PATTERNS)),
re_path(r'^', include(REDIRECT_PATTERNS)),
re_path(r'^', include(EVENT_PATTERNS)),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但是现在我们在127.0.0.1:8000/frontend收到了这个错误:
/ frontend /'companies'中的NoReverseMatch不是注册名称空间
在
'companies:%s-detail' % self.route_name, args=(self.slug, ))
这是逻辑。所以我尝试配置这样的网址
re_path(r'^', include(COMPANY_PATTERNS, namespace='companies')),
但得到了
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
阅读文档,似乎我应该使用
re_path(r'^', include((COMPANY_PATTERNS, app_name), namespace='companies')),
让我们走吧,但我们又回到了这一点:
/ frontend /'companies'中的NoReverseMatch不是注册名称空间
所以我坐下来试图添加前端:在公司工作之前!
'frontend:companies:%s-detail' % self.route_name, args=(self.slug, ))
但是,这是一个非常重要的重构工作,是否有更好的方法来包含URL并设置专用命名空间?
答案 0 :(得分:1)
对于Django 2.0,这应该是:
temppatterns = [
# Admin Sites
path('admin/', admin.site.urls),
# Organisation Sites
path('frontend/', include('frontend.urls')),
# 1st Platform
path('search/', include('search.urls')),
# 2nd Platform
path('international/', include('international.urls')),
]
依旧......
看url()
函数替换为path()
,regexp strings
替换为strings
你读过docs了吗?
另外,请参阅include()
docs以了解可以将哪个对象传递给此函数
答案 1 :(得分:0)
您是否尝试将名称空间添加到模式列表?
REDIRECT_PATTERNS = ( [
url(r'^actualite/(?P<pk>\d+)/(?P<slug>[-\w]+)/$',
views.OldBlogRedirectView.as_view(),
name='blog-redirect'),
.......
url(r'^ressources$',
RedirectView.as_view(
url=reverse_lazy('frontend:doc'), permanent=True)),
] , 'redirects')
...
urlpatterns = [
re_path(r'^', include(REDIRECT_PATTERNS)),
...
我认为,这是你的第一个ImproperlyConfigured
例外抱怨的内容
还有另一种方法(在py_dude的答案中显示);两者都在docs中解释。