英文:
Provide the namespace argument to include() instead on django 3.0
问题
在Django 3.0中,这会产生错误:
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
我应该如何更改 url(r'^admin/', include(admin.site.urls))
?我尝试查看文档,
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include(myapp.views)),
]
英文:
In Django 3.0 this gives the error:
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
How should I change url(r'^admin/', include(admin.site.urls))
? I tried to look at the documentation,
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include(myapp.views)),
]
答案1
得分: 3
从管理员URL中删除include
并使用path
:
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', myapp.views),
]
参考这个django 3文档。
英文:
remove include from admin urls and use path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('myapp.views')),
]
refer this django 3 doc.
答案2
得分: 0
问题出在 include(myapp.views)
:
- 首先,你应该包含一个字符串 (
include('myapp.views')
),而不是实际的模块。这就是导致错误的原因。 - 但第二点,你不应该包含视图,而是应该包含其他的 URL 模式。所以,除非
myapp.views
包含顶级的urlpatterns
,否则也会出错。
最后,正如 @c.grey 指出的那样,使用 path()
,而不是 url()
。
英文:
The problem is with include(myapp.views)
:
- First you should include a string (
include('myapp.views')
), not the actual module. That's what is causing the error. - But second, you don't include views, you include other url patterns. So unless
myapp.views
contains a top-levelurlpatterns
, you'll get an error as well.
Finally, as @c.grey pointed out, use path()
, not url()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论