source: mauRepo/dj_security/trunk/dj_security/views/my_view.py @ 8674

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security/trunk/dj_security/views/my_view.py@8674
Revision 8674, 1.9 KB checked in by mnagni, 7 years ago (diff)

Does not use HTTPResponseRedirect but a "meta refresh" from the logged_in.html page

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 29 Oct 2012
3
4@author: mnagni
5'''
6from django.contrib.auth import authenticate, login
7from django.shortcuts import render_to_response
8from dj_security.login_form import LoginForm
9from django.core.context_processors import csrf
10import logging
11
12# Get an instance of a logger
13logger = logging.getLogger(__name__)
14
15def error_handle(request, context):
16    form = LoginForm()
17    context['form'] = form
18    context.update(csrf(request))
19    return render_to_response('login.html', context)
20
21def my_login(request):       
22    context = {}
23    if request.method == 'POST': # If the form has been submitted...
24        form = LoginForm(request.POST) # A form bound to the POST data
25        context['form'] = form
26        if form.is_valid(): # All validation rules pass
27            username = request.POST['username']
28            password = request.POST['password']
29            user = authenticate(username=username, password=password)           
30            if user is not None:
31                if user.is_active:
32                    login(request, user)
33                    context['r'] = request.GET.get('r', '')
34                    return render_to_response('logged_in.html', context)           
35                else:
36                    # Return a 'disabled account' error message
37                    context['error'] = u'account disabled'
38                    return error_handle(request, context)
39            else:
40                # Return an 'invalid login' error message.
41                context['error'] = u'invalid login'               
42                return error_handle(request, context)
43        else:
44            context['error'] = u'form is invalid'
45            return error_handle(request, context)
46    else:
47        # An unbound form
48        form = LoginForm()
49        context['form'] = form
50        context.update(csrf(request))
51        return render_to_response('login.html', context)
Note: See TracBrowser for help on using the repository browser.