简体   繁体   中英

Trying to trace a circular import error in Django Python

I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django, it is giving me this error message:

django.core.exceptions.ImproperlyConfigured: The included URLconf 'starsocial.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.

The issue started when I added a new app which has a urls.py like the following:

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'),

]

My project urls.py has a line which points to the app and looks like the following code:

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')

]

My application's view looks like the following:

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'

My project's view looks like:

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'

Can anyone please help me identify where I could possibly be going wrong.

You are importing auth.urls twice. Remove url(r'^accounts/', include('django.contrib.auth.urls')) from your project's urls.py

I am importing wrong URL configuration, instead of 'reverse' I should import 'reverse_lazy', change

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'

to

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

from . import forms
# Create your views here.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM