提供namespace参数给include()而不是在django 3.0中。

huangapple go评论75阅读模式
英文:

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-level urlpatterns, you'll get an error as well.

Finally, as @c.grey pointed out, use path(), not url().

huangapple
  • 本文由 发表于 2020年1月6日 20:25:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定