added login page

This commit is contained in:
iou1name 2018-08-10 18:55:42 -04:00
parent 1b8ee3d0f3
commit 8df4347932
9 changed files with 62 additions and 0 deletions

0
login/__init__.py Normal file
View File

3
login/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
login/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class LoginConfig(AppConfig):
name = 'login'

View File

@ -0,0 +1,11 @@
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block content %}
<h1>Login</h1>
<form method="post" action="{{ url('login:index') }}">
{{ csrf_input }}
<input type="text" placeholder="Username" name="username" maxlength="20" required/><br />
<input type="password" placeholder="Password" name="password" maxlength="1024" required/><br />
<input type="submit" value="Log in" name="submit"/>
</form>
{% endblock %}

View File

3
login/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
login/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

12
login/urls.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
"""
Login app URL configuration.
"""
from django.urls import path
from . import views
app_name = 'login'
urlpatterns = [
path('', views.index, name='index'),
]

25
login/views.py Normal file
View File

@ -0,0 +1,25 @@
#!/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
def index(request):
"""
The login page.
"""
if request.method == "GET":
context = {}
return render(request, 'login/index.html', context)
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
messages.success(request, "Logged in")
return redirect('homepage:index')
else:
messages.error(request, "Invalid credentials")
return redirect('login:index')