0

I'm trying to display errors when a user try to login/register with wrong information

I have this codes but errors does not show in browser

I've wrote this code by looking to another project that I done when I was learning Django but now it's not working!

hers is codes of my project

views.py :

def loginPage(request):
    if request.user.is_authenticated:
        return redirect('/')

    form = loginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('/')

    content = {
        "form": loginForm,
        "title": "Login",
    }
    return render(request, 'login.html', content)

#---------------------------------------------

def registerPage(request):
    if request.user.is_authenticated:
        return redirect('/')
    form = registerForm(request.POST or None)

    content = {
        "form": registerForm,
        "title": "Register",
    }

    if form.is_valid():
        username = form.cleaned_data.get('username')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        password2 = form.cleaned_data.get('password2')
        User.objects.create_user(username=username, email=email, password=password)
        return redirect('/')

    return render(request, 'register.html', content)

forms.py :

class loginForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={
        "class": "form-control",
    }))
    password = forms.CharField(widget=forms.PasswordInput(attrs={
        "class": "form-control",
    }))


class registerForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={
        "class": "form-control",
    }))
    email = forms.EmailField(widget=forms.EmailInput(attrs={
        "class": "form-control",
    }))
    password = forms.CharField(widget=forms.PasswordInput(attrs={
        "class": "form-control",
    }))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={
        "class": "form-control",
    }), label='Password Confirm')

    def clean_username(self):
        user_name = self.cleaned_data.get("username")
        user_exists = User.objects.filter(username=user_name).exists()
        if user_exists:
            raise forms.ValidationError("Username is in user")
        return user_name

    def clean_email(self):
        email = self.cleaned_data.get('email')
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError('Email is in use!!')
        return email

    def clean_password2(self):
        pass1 = self.cleaned_data.get('password')
        pass2 = self.cleaned_data.get('password2')
        if pass1 != pass2:
            raise forms.ValidationError('Passwords does not match')
        return pass2

html(it's for register but login is the same) :

    <div class="container">
        <div class="row justify-content-center">
            <h3 class="text-center">{{ title }}</h3>
            <hr>
            <div class="col-md-4">
                <form method="post">
                    {% csrf_token %}
                    {% for error in form.username.errors %}
                        <p style="color: red">{{ error }}</p>
                    {% endfor %}

                    {% for error in form.email.errors %}
                        <p class="bg-danger text-light">{{ error }}</p>
                    {% endfor %}

                    {% for error in form.password.errors %}
                        <p class="bg-danger text-light">{{ error }}</p>
                    {% endfor %}

                    {% for error in form.password2.errors %}
                        <p class="bg-danger text-light">{{ error }}</p>
                    {% endfor %}

                    <label for="{{ form.username.id_for_label }}">{{ form.username.label }}</label>
                    {{ form.username }}
                    <br>

                    <label for="{{ form.email.id_for_email }}">{{ form.email.label }}</label>
                    {{ form.email }}
                    <br>

                    <label for="{{ form.password.id_for_label }}">{{ form.password.label }}</label>
                    {{ form.password }}
                    <br>

                    <label for="{{ form.password2.id_for_label }}">{{ form.password2.label }}</label>
                    {{ form.password2 }}

                    <hr>
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-outline-success">Login</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
2
  • Instead "form": loginForm use "form": form and the same in your registerPage: "form": registerForm > "form": form Commented Feb 26, 2021 at 10:38
  • what a mistake I made . thank you Commented Feb 26, 2021 at 10:45

1 Answer 1

2

You add to your context unbound Class loginForm - See The Forms API

Use bound form form = registerForm(request.POST) and form.is_valid().

So in your views change it your :

"form": loginForm to "form": form

"form": registerForm to "form": form

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

Comments

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.