blechreiz-website/website/views.py

46 lines
1.5 KiB
Python

# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.utils import simplejson
def home_view(request):
return render( request, 'website/mainpage.html' )
def logout_view(request):
logout( request )
return redirect( login_view )
def login_view(request):
if request.method == 'POST': # If the form has been submitted...
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
result = dict()
result['err'] = ""
result['redirect'] = ""
if user is not None:
if user.is_active:
if not request.POST.get('remember', None):
request.session.set_expiry( 0 )
login(request, user)
result['redirect'] = reverse( home_view )
else:
result['err'] = "Dein Account wurde deaktiviert."
# Return a 'disabled account' error message
else:
result['err'] = "Falscher Benutzername oder falsches Kennwort."
return HttpResponse( simplejson.dumps(result), mimetype='application/json' )
else:
# TODO check if already logged in --------------------!!!!
return render( request, 'website/login.html' )