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.