added authentication

This commit is contained in:
2015-06-19 15:09:50 +01:00
parent e6791bf41a
commit 16c74cebe9
11 changed files with 74 additions and 11 deletions

0
comic_auth/__init__.py Normal file
View File

3
comic_auth/admin.py Normal file
View File

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

View File

3
comic_auth/models.py Normal file
View File

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

View File

@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block title %}CBreader Login{% endblock %}
{% block content %}
<div class="col-md-4 col-md-offset-4">
{% if error %}
<div class="alert alert-danger" role="alert"><p>Your username and password didn't match. Please try again.</p></div>
{% endif %}
<form method="post" class="form-signin">
{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputUser" class="sr-only">Username</label>
<input type="text" name="user" id="inputUser" class="form-control" placeholder="Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{% endblock %}

3
comic_auth/tests.py Normal file
View File

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

28
comic_auth/views.py Normal file
View File

@@ -0,0 +1,28 @@
from django.shortcuts import render, redirect
from django.shortcuts import RequestContext
from django.contrib.auth import authenticate, login, logout
def comic_login(request):
if request.POST:
user = authenticate(username=request.POST['user'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
if request.GET.has_key('next'):
return redirect(request.GET['next'])
else:
return redirect('/comic/')
else:
context = RequestContext(request, {
'error': True
})
return render(request, 'comic_auth/login.html', context)
else:
context = RequestContext(request, {})
return render(request, 'comic_auth/login.html', context)
def comic_logout(request):
logout(request)
return redirect('/login/')