Redirect not working in view

I have a basic app and then I have an accounts app to handle user authentication. I have created the register view and the login view but once I log in it does not want to redirect me to the page I specify.

Here is the login view

from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model, authenticate, login, logout
from django.views import View
from .forms import RegisterForm, LoginForm, ForgotPasswordForm

def login(request):
        form = LoginForm(request.POST or None)
        if form.is_valid():
                email = form.cleaned_data.get('email')
                password = form.cleaned_data.get('password')
                user = authenticate(email=email, password=password)
                login(request, user)
                return redirect('/app')

        context = {
                'form':form,
        }
        return render(request, "login.html", context)

and here is the form

class LoginForm(forms.Form):
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'email-input'}))
    password = forms.CharField(label="Password", widget=forms.PasswordInput(attrs={'class':'password-input'}))

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

        if email and password:
            user = authenticate(email=email, pasword=password)
            if not user:
                raise forms.ValidationError('This user does not exist, please <a href="/register">register</a>')
            if not user.check_password(password):
                raise forms.ValidationError('You have entered the wrong password. <a href="/forgot-password">Did you forget your password?</a>')
            if not user.is_active:
                raise forms.ValidationError('This account is not active. Please <a href="/support/contact">contact support</a>')
            return super(LoginForm, self).clean(*args, **kwargs)

and here is the template

            <form action="" method="post" >
                {% csrf_token %}
                <div class="fieldWrap">
                    <div class="row">
                        <label for="{{ form.email.id_for_label }}" class="label">Email</label>
                        {{ form.email}}
                    </div>
                    <p class="help is-danger">{{ form.username.errors }}</p>
                </div>
                <div class="fieldWrap">
                    <div class="row">
                        <label for="{{ form.password.id_for_label }}" class="label">Password</label>
                        {{ form.password}}
                    </div>
                    <p class="help is-danger">{{ form.password.errors }}</p>
                </div>
                <div class="fieldWrap loginFormButton">
                    <button id="loginFormButton">Log in</button>
                </div>
            </form>

I cannot get it to redirect to the URL /app.

Provide the error you’re getting.

I am not getting any errors. Nothing happens after I click login the page just stays like it is on the login page. I do not see anything happening. This is the URL http://127.0.0.1:8000/accounts/login/

How do I know if I am logged in or not?

Sorry I am new to this

The django.contrib.auth.authenticate method doesn’t accept email as a keyword argument for performing authentication.

You can either rewrite this yourself, or, what I would recommend, is using one of the existing packages designed to use the email address for authentication.

Take a look at one or more of:

django-allauth, django-username-email, or django-use-email-as-username.

Note: I wouldn’t just grab one of these to start using them. I do, at a minimum, suggest you at least browse through the source code to see what they’re doing.
But one or more of these should be good enough for what you’re trying to do.

Ken

Hi Ken

Thank you for the advice and I will read through them and see how I can make this work.

On another note, and one more of curiosity than of any need to see it, I would have expected an error would have been thrown and visible in the console session where you’re running runserver. I’d try this again, and check both the console log, and any custom django logging you may have defined.