I'm trying to save some form inputs into the DB by extending the default auth library. For some reason, it is only saving the "email" in the DB. The password field was not stored and no error was shown. What am I doing wrong? Please help! Thanks!
#views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from Accounts.forms import CreateUserForm
# Create your views here.
def registerPage(request):
#debugDump
form = CreateUserForm
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
context = {
'form':form
}
#return HttpResponse(form.error_messages)
return render(request, 'accounts/register.html', context)
#Accounts\forms
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth import get_user_model
from django.http import HttpResponse
User = get_user_model()
class CreateUserForm(forms.ModelForm):
password1 = forms.CharField()
password2 = forms.CharField()
class Meta:
model = User
fields = ['email','password1', 'password2']
def clean_email(self):
'''
Verify email is available.
'''
email = self.cleaned_data.get('email')
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError("email is taken")
return email
def clean(self):
'''
Verify both passwords match.
'''
cleaned_data = super().clean()
password1 = cleaned_data.get("password1")
password2 = cleaned_data.get("password2")
if password1 is not None and password1 != password2:
self.add_error("password2", "Your passwords must match")
return cleaned_data
return HttpResponse(request)must come afterform.save()user_form = form.save()and thenuser.set_password(user_form.password)and thenuser.save()