Пример #1
0
def login(request):
    def errorHandle(error):
        form = LoginForm()
        return render_to_response('barsapp/login.html',
            {'error' : error, 'form' : form,},
            context_instance=RequestContext(request))

    if request.method == 'POST': # If the form has been submitted...
        form = LoginForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            username = request.POST['username']
            password = request.POST['password']
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    # Redirect to a success page.
                    auth.login(request, user)
                    return HttpResponseRedirect('/secured/')
                else:
                    # Return a 'disabled account' error message
                    error = u'account disabled'
                    return errorHandle(error)
            else:
                # Return an 'invalid login' error message.
                error = u'invalid login'
                return errorHandle(error)
        else: 
            error = u'form is invalid'
            return errorHandle(error)		
    else:
        form = LoginForm() # An unbound form
        return render_to_response('barsapp/login.html', {'form': form,},
            context_instance=RequestContext(request))
Пример #2
0
def _authentication(request, context):
    if request.method == 'POST': # If the form has been submitted...
        form = LoginForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            username = request.POST['username']
            password = request.POST['password']
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    return True
                else:
                    # Return a 'disabled account' error message
                    context['login_error'] = 'Compte désactivé.'
                    return False
        # Return an 'invalid login' error message.
        context['login_error'] = u'Login ou mot de passe incorrect.'
    return False