gpt4 book ai didi

python - Django 2.0 |包含多个 urls conf 命名空间

转载 作者:行者123 更新时间:2023-12-04 16:59:55 25 4
gpt4 key购买 nike

这个“问题”与: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)

这是以前的前端 URL 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 得到了这个错误:

NoReverseMatch at /frontend/ 'companies' is not a registered namespace





'companies:%s-detail' % self.route_name, args=(self.slug, )) 


这是逻辑。所以我尝试配置这样的 URL

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')),


所以让我们走吧,但我们又回到了这个:

NoReverseMatch at /frontend/ 'companies' is not a registered namespace



所以我坐下来尝试添加前端:在工作的公司之前!

'frontend:companies:%s-detail' % self.route_name, args=(self.slug, )) 


然而,这是一项非常大的重构工作,有没有更好的方法来包含 URL 并设置专用命名空间?

最佳答案

您是否尝试将命名空间添加到模式列表中?

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 中进行了解释.

关于python - Django 2.0 |包含多个 urls conf 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com