在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), ]更多推荐
发布评论