'i couldn't fix the problem createview is missing a queryset
ImproperlyConfigured at /register/
SignUpView is missing a QuerySet. Define SignUpView.model, SignUpView.queryset, or override SignUpView.get_queryset().
Request Method: GET
Request URL: http://localhost:8000/register/
Django Version: 3.0.7
Exception Type: ImproperlyConfigured
Exception Value:
SignUpView is missing a QuerySet. Define SignUpView.model, SignUpView.queryset, or override SignUpView.get_queryset().
Exception Location: D:\ch\env\lib\site-packages\django\views\generic\detail.py in get_queryset, line 69
Python Executable: D:\ch\env\Scripts\python.exe
Python Version: 3.8.3
Python Path:
['D:\ch\ch',
'D:\ch\env\Scripts\python38.zip',
'c:\users\dell\appdata\local\programs\python\python38\DLLs',
'c:\users\dell\appdata\local\programs\python\python38\lib',
'c:\users\dell\appdata\local\programs\python\python38',
'D:\ch\env',
'D:\ch\env\lib\site-packages']
Server time: Fri, 12 Jun 2020 15:12:00 +0000
views.py
from django.shortcuts import render
from .forms import SignUpForm
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.
from django.views.generic import TemplateView, CreateView
class HomeView(TemplateView):
template_name = 'common/home.html'
class DashboardView(LoginRequiredMixin,TemplateView):
template_name = 'common/home.html'
login_url = reverse_lazy('home')
class SignUpView(CreateView):
from_class = SignUpForm
success_url = reverse_lazy('home')
template_name = 'common/register.html'
urls.py
from django.contrib import admin
from django.urls import path
from apps.common.views import HomeView, SignUpView, DashboardView
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomeView.as_view(), name='home'),
path('register/', SignUpView.as_view(), name='register'),
path('login/', auth_views.LoginView.as_view(
template_name='common/login.html'
),
name='login'),
path('logout/', auth_views.LogoutView.as_view(
next_page='home'
),
name='logout'),
path('dashboard/', DashboardView.as_view(), name='dashboard'),
]
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Optional')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional')
email = forms.EmailField(max_length=254, help_text='Enter a valid email address')
class Meta:
model = User
fields = [
'username',
'first_name',
'last_name',
'email',
'password1',
'password2',
]
Solution 1:[1]
You need to specify the model attribute (or the queryset or get_queryset) of the CreateView:
class SignUpView(CreateView):
model = User
from_class = SignUpForm
success_url = reverse_lazy('home')
template_name = 'common/register.html'
Solution 2:[2]
Form is wrongly spelt in the SignUpView class.Fix it and it should work fine.
Solution 3:[3]
Check the spelling of the information after class instantiation.
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic
class SignUpView(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
I made the mistake of spelling "form_class" as "from_class".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
| Solution 2 | celorhmbib56 |
| Solution 3 | Andrekweku |
