From aeaaa1df2b0e168ac3103d44a8dbc28c9e7bd1ac Mon Sep 17 00:00:00 2001 From: iou1name Date: Sun, 12 Aug 2018 14:58:18 -0400 Subject: [PATCH] refactored login view to be more standard --- login/views.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/login/views.py b/login/views.py index f273940..c3addfc 100644 --- a/login/views.py +++ b/login/views.py @@ -14,18 +14,18 @@ def index(request): """ if request.user.is_authenticated: return redirect('homepage:index') - if request.method == "GET": + 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}) - 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})