配置不当的循环导入错误Django2 Python3

时间:2019-09-28 23:53:53

标签: django python-3.x

即使将语法更新为django2 / python3兼容,我仍然会遇到此错误。反复检查后,我看不到任何误名。

我尝试使用url而不是path和django 1,但这仍然不能解决问题

hackuta / urls.py:

from django.urls import include, path
from django.contrib import admin
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.HomePage.as_view(),name='home'),
    path('first_app/',include('first_app.urls'),namespace = 'first_app'),
    path('first_app/',include('django.contrib.auth.urls')),
    path('test/',views.TestPage.as_view(),name='test'),
    path('thanks/',views.ThanksPage.as_view(), name='thanks')
]

尝试迁移时显示错误:

File "/home/bbwslayer/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 593, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'hackuta.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

如何解决此问题以便顺利迁移?

编辑: 我遵循了其中一项评论的建议,事实证明,当我评论以下行时,我可以很好地进行迁移:

    path('first_app/',include('first_app.urls'),namespace = 'first_app'),

我认为包括first_app.urls的代码是合适的:

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

app_name = 'first_app'
urlpatterns = [
    path('login/',auth_views.LoginView.as_view(template_name = 'first_app/login.html'),name = 'login'),
    path('logout/',auth_views.LogoutView.as_view(),name='logout'),
    path('signup/',auth_views.SignUp.as_view(),name='signup')
]

2 个答案:

答案 0 :(得分:0)

确保每个urls.py中都有 urlpatterns ,如下所示:

urlpatterns = []

此外,请确保在urlpatterns的{​​{1}}中添加了右括号,因为您在问题中没有使用它!

答案 1 :(得分:0)

应该是:

path('first_app/', include('first_app.urls', namespace='first_app')),

代替:

path('first_app/', include('first_app.urls'), namespace='first_app'),

namespaceinclude而不是path的参数。