I am trying to create a page where login and registration are in the same template in django, the login form is working correctly but the registration form is not submitting the registration form is not validated in the views i have tried using django's in build UsercreationForm but that doesn't work either

views.py

def login_register(request):
    if request.method == "POST":
        form_log = UserLoginForm(data=request.POST or None)
        form_reg = UserRegistrationForm(data=request.POST or None)
        if 'sign_up' in request.POST:
            if form_reg.is_valid():
                user = form_reg.save(commit=False)
                password = form_reg.cleaned_data.get('password1')
                user.set_password(password)
                user.save()
                login(request, user)

                return redirect('network:homepage')
        elif 'sign_in' in request.POST:
            if form_log.is_valid():
                username = form_log.cleaned_data.get('username')
                password = form_log.cleaned_data.get('password')
                user = authenticate(request, username=username, password=password)
                login(request, user)
                if user is not None:
                    login(request, user)
                    return HttpResponseRedirect(reverse('network:homepage'))

    else:
        form_reg = UserRegistrationForm()
        form_log = UserLoginForm()
    return render(request,"network/bothpage.html", {'form_reg': form_reg, 'form_log': form_log})

forms.py

lass UserLoginForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}), label='Username')
    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}), label="Password")

    class Meta:
        model = User
        fields = ['username', 'password']

    def clean(self, *args, **kwargs):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            user = authenticate(username=username, password=password)
            if not user:
                raise forms.ValidationError('This user does not exist')
            if not user.check_password(password):
                raise forms.ValidationError('Incorrect Password')
            if not user.is_active:
                raise forms.ValidationError('User is Not active')
        return super(UserLoginForm, self).clean(*args, **kwargs)


class UserRegistrationForm(forms.ModelForm):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}), label='Username')
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Email'}))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}), label="Password")
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password Conformation'}), label="Password Conformation")

    class Meta:
        model = User
        fields = [
            'username',
            'email',
            'password1',
            'password2',
        ]

    def clean_username(self):
        username = self.cleaned_data.get('username')
        username_qs = User.objects.filter(username=username)
        if username_qs.exists():
            raise forms.ValidationError(
                "This Username already exists"
            )

    def clean_password(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 != password2:
            raise forms.ValidationError("passwords must match")

template.html

<form method="POST">
        {% csrf_token %}
            .....
            ....
            <span>or use your account</span>
            {{ form_log.errors }}
            {{ form_log.non_field_errors }}
            {{ form_log.username.error}}
            {{ form_log.username }}
            {{ form_log.password.error}}
            {{form_log.password}}

            <button type = "submit" value = 'sign_in' name= "sign_in">Sign In</button></form>
<form method="POST">
        {% csrf_token %}
            <h1>Create Account</h1>
            {{ form_reg.errors }}
            {{ form_reg.non_field_errors }}
            {{ form_reg.username.error }}
            {{ form_reg.username }}
            {{ form_reg.email.error }}
            {{ form_reg.email }}
            {{ form_reg.password1.error}}
            {{form_reg.password1}}
            {{ form_reg.password2.error}}
            {{form_reg.password2}}
            <button type = "submit" value = 'sign_up' name= "sign_up">Sign Up</button>
        </form>

0 Answers