尝试在Django Python中跟踪循环导入错误

时间:2020-02-02 19:44:29

标签: python django

我知道有人问过很多循环导入错误,但是经过这些问题之后,我仍无法解决我的问题。当我尝试在Django中运行服务器时,它显示了以下错误消息:

django.core.exceptions.ImproperlyConfigured:包含的URLconf'starsocial.urls'似乎没有任何模式。如果您在文件中看到有效的模式,则说明问题可能是由循环导入引起的。

当我添加一个具有如下urls.py的新应用程序时,问题就开始了:

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views

app_name = 'accounts'
urlpatterns = [
    url(r'login/$', 
        auth_views.LoginView.as_view(template_name='accounts/login.html'), 
        name='login'),
    url(r'logout/$',auth_views.LogoutView.as_view(), name='logout'),
    url(r'signup/$',views.SignUp.as_view(), name='signup'),

]

我的项目urls.py中有一行指向应用程序,看起来像以下代码:

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

urlpatterns = [
    url(r'^admin/',admin.site.urls),
    url(r'^$', views.HomePage.as_view(), name='home'),
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^test/$', views.TestPage.as_view(), name='test'),
    url(r'^thanks/$', views.ThanksPage.as_view(), name='thanks')

]

我的应用程序的视图如下所示:

from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView

from . import forms 
# Create your views here.

class SignUp(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse('login')
    template_name = 'accounts/signup.html'

我的项目的视图如下:

from django.views.generic import TemplateView

class TestPage(TemplateView):
    template_name = 'test.html'

class ThanksPage(TemplateView):
    template_name = 'thanks.html'

class HomePage(TemplateView):
    template_name = 'index.html'

任何人都可以帮助我确定我可能在哪里出错了。

2 个答案:

答案 0 :(得分:0)

您两次导入auth.url。从项目的url(r'^accounts/', include('django.contrib.auth.urls'))

中删除urls.py

答案 1 :(得分:0)

我导入的URL配置错误,而不是“反向”,我应该导入“ reverse_lazy”, 更改

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/number_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >


        <EditText
            android:id="@+id/numberone"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numbertwo"
            />


        <EditText
            android:id="@+id/numbertwo"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numberthree"
            />


        <EditText
            android:id="@+id/numberthree"
            android:layout_width="@dimen/edit_text"
            android:layout_height="@dimen/edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/numberfour"
            />


        <EditText
            android:id="@+id/numberfour"
            android:layout_width="@dimen/safe_edit_text"
            android:layout_height="@dimen/safe_edit_text_h"
            android:inputType="number"
            android:maxLength="1"
            android:nextFocusDown="@id/btn_confirmcode"
            />


    </androidx.constraintlayout.widget.ConstraintLayout>

from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView

from . import forms 
# Create your views here.

class SignUp(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse('login')
    template_name = 'accounts/signup.html'