Search code examples
pythondjangodjango-viewsdjango-urls

ImportError Django cannot import name 'SignupView'


So I imported the 'LoginView, LogoutView' but when I tried to import the 'SignupView' I got the following error: ImportError: cannot import name 'SignupView' from 'django.contrib.auth.views' (C:\Users\USER\Desktop\djang0\venv\Lib\site-packages\django\contrib\auth\views.py)

This is what I believe to be the necessary code.

#my views.py

from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect, reverse
from django.core.mail import send_mail
from django.views import generic
from django.http import HttpResponse
from.models import Lead, Agent
from.forms import LeadForm, LeadModelForm

class SignupView(generic.CreateView):
    template_name = "registration/signup.html"
    form_class = UserCreationForm

    def get_success_url(self):
        return reverse("login")


#my app urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView, SignupView
from django.urls import path, include
from leads.views import hero_page, HeroPageView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', HeroPageView.as_view(), name="hero-page"),
    path('leads/', include('leads.urls', namespace="leads")),
    path('login/', LoginView.as_view(), name="login"),
    path('logout/', LogoutView.as_view(), name="logout"),
    path('signup/', SignupView.as_view(), name="signup"),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

I don't know what to try, I'm kind of a beginner in Django. I've done a lot of research on multiple sites and articles but only person I've seen who had a similar problem had to remove the 'views' from the 'from django.contrib.auth.views' because they ran on an older version of Django which I believe to have been 2.0.1 but I'm running on Django version %.0.1 which I hear is valid. Any help will be highly appreciated and please explain in a way any beginner can understand.


Solution

  • You defined the SignupView in your own views.py, hence you import it from there:

    from app_name.views import SignupView
    from django.contrib.auth.views import LoginView, LogoutView  # no SignupView
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', HeroPageView.as_view(), name='hero-page'),
        path('leads/', include('leads.urls', namespace='leads')),
        path('login/', LoginView.as_view(), name='login'),
        path('logout/', LogoutView.as_view(), name='logout'),
        path('signup/', SignupView.as_view(), name='signup'),
    ]

    That being said, your view seems to only inject a different template and a different success URL, you can just plug these into the CreateView:

    from django.contrib.auth.forms import UserCreationForm
    from django.contrib.auth.views import LoginView, LogoutView  # no SignupView
    from django.urls import reverse_lazy
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', HeroPageView.as_view(), name='hero-page'),
        path('leads/', include('leads.urls', namespace='leads')),
        path('login/', LoginView.as_view(), name='login'),
        path('logout/', LogoutView.as_view(), name='logout'),
        path(
            'signup/',
            CreateView.as_view(
                template_name='registration/signup.html',
                form_class=UserCreationForm,
                success_url=reverse_lazy('login'),
            ),
            name='signup',
        ),
    ]