0

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
4
  • This return HttpResponse(request) must come after form.save() Commented Feb 1, 2022 at 7:32
  • Yes, I know that. I put that there to debug and removed it before I ran the code. A new record was inserted but just the email. Commented Feb 1, 2022 at 7:41
  • You have explicitly set password like this: user_form = form.save() and then user.set_password(user_form.password) and then user.save() Commented Feb 1, 2022 at 7:43
  • Ah okay. Will try that. Thanks! Commented Feb 1, 2022 at 7:48

1 Answer 1

1

please review this tutorial:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
...

https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html

Sign up to request clarification or add additional context in comments.

2 Comments

Hi! this is super helpful, thank you! but I'm getting the following errors: AttributeError at /register/ Manager isn't available; 'auth.User' has been swapped for 'Accounts.User' Request Method: POST Request URL: 127.0.0.1:8000/register Django Version: 4.0.1 Exception Type: AttributeError Exception Value: Manager isn't available; 'auth.User' has been swapped for 'Accounts.User'
If you override user account model: change this code : from django.contrib.auth.models import User to : from accounts.models import User but the best solutions if an user model swapped : from django.contrib.auth import get_user_model User = get_user_model() for more details: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.