在 include() 中使用命名空间时出现关于 app_name 的 ImproperlyConfiguredError

2024-04-06

我目前正在尝试 Django。我用namespace我的一个论证include()s 在 urls.py 中。当我运行服务器并尝试浏览时, 我收到这个错误。

File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
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.

这些是我的 urls.py 文件:

#project/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^reviews/', include('reviews.urls', namespace='reviews')),
    url(r'^admin/', include(admin.site.urls)),
]

and

#app/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /
    url(r'^$', views.review_list, name='review_list'),
    # ex: /review/5/
    url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
    # ex: /wine/
    url(r'^wine$', views.wine_list, name='wine_list'),
    # ex: /wine/5/
    url(r'^wine/(?P<wine_id>[0-9]+)/$', views.wine_detail, name='wine_detail'),
]

我要通过什么app_name如错误消息中所述?


检查文档是否包含here https://docs.djangoproject.com/en/dev/ref/urls/#include.

您所做的不是传递要包含的参数的可接受的方式。你可以这样做:

url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 include() 中使用命名空间时出现关于 app_name 的 ImproperlyConfiguredError 的相关文章

随机推荐