Titivillus/login/views.py

32 lines
817 B
Python

#!/usr/bin/env python3
"""
/login app views.
"""
from django.contrib import messages
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login
from .forms import LoginForm
def index(request):
"""
The login page.
"""
if request.user.is_authenticated:
return redirect('homepage:index')
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(
request,
username=form.cleaned_data.get('username'),
password=form.cleaned_data.get('password'))
if user is not None:
login(request, user)
messages.success(request, "Logged in")
return redirect('homepage:index')
messages.error(request, "Invalid credentials")
else:
form = LoginForm()
return render(request, 'login/index.html', {'form': form})