在include()中指定命名空间而不提供app

编程入门 行业动态 更新时间:2024-10-07 00:25:48
在include()中指定命名空间而不提供app_name(Specifying a namespace in include() without providing an app_name)

models.py

from django.conf.urls import include, url app_name = "Review" urlpatterns = [ url(r'^books/', include("Review.urls", namespace='reviews')), ]

评论\ urls.py

from django.conf.urls import include, url from django.contrib import admin from .views import ( ReviewUpdate, ReviewDelete, ) urlpatterns = [ url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'), url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'), ]

我在urlpatterns之前提供app_name。 但它在运行我的代码时给了我错误。 错误如下:

File "E:\workspace\python\web\Book_Review_App\Book\urls.py", line 13, in <module> url(r'^books/', include("Review.urls", namespace='reviews')), File "E:\workspace\python\web\Book_Review_App\venv\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.

请帮忙。

models.py

from django.conf.urls import include, url app_name = "Review" urlpatterns = [ url(r'^books/', include("Review.urls", namespace='reviews')), ]

Review\urls.py

from django.conf.urls import include, url from django.contrib import admin from .views import ( ReviewUpdate, ReviewDelete, ) urlpatterns = [ url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'), url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'), ]

I am providing app_name before my urlpatterns. But it's giving me error while running my code. The errors are given below:

File "E:\workspace\python\web\Book_Review_App\Book\urls.py", line 13, in <module> url(r'^books/', include("Review.urls", namespace='reviews')), File "E:\workspace\python\web\Book_Review_App\venv\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.

Please help.

最满意答案

app_name需要在应用程序的urls.py中设置,而不是主urls.py.

在review.urls.py中添加以下内容,

from django.conf.urls import include, url from django.contrib import admin from .views import ( ReviewUpdate, ReviewDelete, ) app_name = 'Review' urlpatterns = [ url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'), url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'), ]

并从主网址中删除app_name

编辑:对于操作中提到的操作系统问题,在主urls.py中

from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]

The app_name needs to be set in your app's urls.py not the main urls.py.

In review.urls.py add the following,

from django.conf.urls import include, url from django.contrib import admin from .views import ( ReviewUpdate, ReviewDelete, ) app_name = 'Review' urlpatterns = [ url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'), url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'), ]

and remove the app_name from the main urls

EDIT: for the admin issue that the op mentioned in the comments, in the main urls.py

from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]

更多推荐

本文发布于:2023-07-09 10:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   空间   include   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!