I'm receiving the error
"ValueError at /accounts/create/
ModelForm has no model class specified."
Views.py:
from django.shortcuts import render
from .forms import CustomUserCreationForm
from .models import CustomUser
from django.contrib.auth.models import Group
def signupView(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
signup_user = CustomUser.objects.get(username=username)
customer_group = Group.objects.get(name='Customer')
customer_group.user_set.add(signup_user)
else:
form = CustomUserCreationForm()
return render(request, 'signup.html', {'form':form})
I can't find any the error for the life of me, any help would be appreciated. Thanks.
Edit::
forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
Meta = CustomUser
fields = UserCreationForm.Meta.fields + ('age',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
Above is the CustomUserCreationForm
In your CustomUser model, in Meta class, you have used Model = CustomUser. Instead you need to use model = CustomUser.
Related
I need to register a user and use it's email as Username. I am getting this error (UNIQUE constraint failed: auth_user.username) when trying to register on the page. I AM NEW TO DJANGO AND PYTHON
My Code is
forms.py File
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from phonenumber_field.formfields import PhoneNumberField
from django.db import transaction
from .models import User
class UserRegistrationForm(UserCreationForm):
name = forms.CharField(max_length=60)
# Username = forms.CharField(max_length=15)
email = forms.EmailField()
class Meta(UserCreationForm.Meta):
models = User
fields = ['name','email','password1','password2']
views.py file
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from .forms import UserRegistrationForm
from django.contrib.auth import get_user_model
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
User = form.save()
else:
form = UserRegistrationForm()
return render(request, 'users/signup.html', {'form': form})
models.py
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
"""docstring for User"""
email = models.EmailField(verbose_name='Email Address', unique=True)
name = models.CharField(max_length=50)
USERNAME_FIELD = 'email'
user_permissions = None
groups = None
REQUIRED_FIELDS = []
def __str__():
return self.name
As per youtube tutorial the user is created with exactly same method but mine doesn't work. Why?
views.py
from django.shortcuts import render,redirect
from .forms import CreateUserForm
def registerView(request):
form=CreateUserForm()
if request.method=="POST":
form=CreateUserForm(request.POST)
if form.is_valid():
form.save()
user=form.cleaned_data.get('username')
messages.success(request,'Account was successfully created for '+ user)
return redirect('login')
context={'form':form}
return render(request,"accounts/register.html", context)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = ['username','email','password1','password2']
Why is a user not created?
I am new to Django, I tried to create a new models forms but while running my server i got this error:
(The form should add a "number" to the Profile database) Thanks for helping
(The form should add a "number" to the Profile database) Thanks for helping
File "C:\Users\xxx\PycharmProjects\web_lead_app\venv\lib\site-packages\django\forms\models.py", line 266, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (number) specified for User
views.py
from .forms import UserRegisterForm, ProfileUpdateForm, UserUpdateForm
from django.contrib.auth.decorators import login_required
#login_required()
def profile(request):
u_form = UserUpdateForm()
p_form = ProfileUpdateForm()
context = {
"u_form": u_form,
"p_form": p_form
}
return render(request, "users/profile.html", context)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["email", "number"]
class ProfileUpdateForm(forms.ModelForm):
number = forms.CharField(max_length=100)
class Meta:
model = Profile
fields = ["number"]
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
number = models.CharField(max_length=100)
def __str__(self):
return f"{self.user.username} Profile"
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["email"]
Their is no number field in User model. So remove from form.
django forms doesn't save email in the database.i try all sort of things but still it is the only thing that is not saving .I need help
https://i.imgur.com/LJymHeS.png
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegistrationForm(UserCreationForm):
email=forms.EmailField()
class meta:
model=User
fields= ['username', 'email_address', 'password1','password2']
views.py code
register(request):
if request.method=='POST':
form=UserRegistrationForm(request.POST)
if form.is_valid():
form.save(commit=False)
username=form.cleaned_data['username']
password1=form.cleaned_data['password1']
password2=form.cleaned_data['password2']
email_address=form.cleaned_data['email']
form.save()
return redirect('/')
else:
form=UserRegistrationForm()
return render(request, 'blog/register.html',{'form':form})
The name of the field is email, not email_address. You thus should change this to:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegistrationForm(UserCreationForm):
email = forms.EmailField()
class meta:
model = User
fields = ['username', 'email']
Furthermore password1 and pasword2 are no model fields either. The UserCreationForm has some logic to compare the fields, and set a password.
In your register view, there is no need to unpack the cleaned data, you can just use form.save(). You can call login(request, user) if you want to automatically login the user you just created:
from django.contrib.auth import login
def register(request):
if request.method=='POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('/')
else:
form = UserRegistrationForm()
return render(request, 'blog/register.html',{'form':form})
You can override the UserAdmin admin with:
# blog/admin.py
from blog.forms import UserRegistrationForm
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
admin.site.unregister(User)
#admin.register(User)
class NewUserAdmin(UserAdmin):
add_form_template = 'blog/register.html'
add_form = UserRegistrationForm
In django documentation the class used in UserCreationForm is Meta. you have to use capital M for meta class.You should get it correct when you have changed to capital M
class Meta:
model = User
fields = ['username', 'email']
Could someone smart explain me please where I have a bug?
I get this error when I would like to send a profile user form
NOT NULL constraint failed: userprofile_userprofile.godzina_id
I have an app "userprofile"
forms.py
from django import forms
from models import UserProfile
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('imie', 'godzina')
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from forms import UserProfileForm
from django.contrib.auth.decorators import login_required
from django.conf import settings
#login_required
def user_profile(request):
if request.method == 'POST':
form = UserProfileForm(request.POST, instance=request.user.profile)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/loggedin')
else:
user = request.user
profile = user.profile
form = UserProfileForm(instance=profile)
args = {}
args.update(csrf(request))
args['form'] = form
return render(request, 'user_profile.html', args)
models.py:
from django.db import models
from django.contrib.auth.models import User
from godzina.models import Godzina
class UserProfile(models.Model):
user = models.OneToOneField(User)
imie = models.CharField(max_length=150)
godzina = models.ForeignKey('godzina.Godzina')
User.profile = property(lambda u:UserProfile.objects.get_or_create(user=u)[0])
You could add null=True to your godzina attribute:
godzina = models.ForeignKey('godzina.Godzina', null=True)