#!/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.method == "GET": form = LoginForm() return render(request, 'login/index.html', {'form': form}) 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") return render(request, 'login/index.html', {'form': form})