0

I am trying to allow people to create an account by submitting a form. This is currently just continuing to a login screen but not creating the actual user or coming up with any errors. Debug is currently enabled.

I have tried adding an else statement after the user_form.is_valid() to ensure it wasn't invalid but the same issue was happening. I am guessing it is an issue with how the form is saving but I am newer to Django, so any help is appreciated.

view:

def home(request):
    if request.user.is_authenticated:
        return redirect('user_dashboard')
    else:
        user_form = CreateUserForm(request.POST or None)
        if request.method == 'POST':
            if user_form.is_valid():
                user_form.save()
        context = {
            'user_form': CreateUserForm,
        }
        return render(request, 'Main/index.html', context)

Model:

class CreateUserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())
    confirm_password = forms.CharField(widget=forms.PasswordInput())
    class Meta:
        model = User
        fields = [
            'username',
            'email',
            'password',
        ]

    def clean_password(self):
        password = self.cleaned_data.get("password")
        confirm_password = self.cleaned_data.get("confirm_password")

        if password != confirm_password:
            raise forms.ValidationError(
                "password and confirm_password does not match"
            )
        else:
            return password
  • Hi Brent, here is a great, comprehensive tutorial - [https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html](https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html) – joshlsullivan Dec 29 '18 at 00:40

1 Answers1

0

Your problem is here :

context = {
        'user_form': CreateUserForm,
    }

It should be :

context = {
        'user_form': user_form,
    }

Cheers :)

mng back
  • 40
  • 1
  • 10