Beispiel #1
0
def login_user(request):
    if request.user.is_authenticated():
        return render(request, 'login/home.html')
    else:
        state = "Please log in below..."
        if request.POST:
            form = LoginForm(request.POST)
            if form.is_valid(): # All validation rules pass
                username_to_try = form.cleaned_data["username"]
                password_to_try = form.cleaned_data["password"]

                user = authenticate(username=username_to_try, password=password_to_try)
                if user is not None:
                    if user.is_active:
                        login(request, user)
                        return render(request, 'login/home.html', {'state':state})
                    else:
                        state = "Your account is not active, please contact the site admin."
                else:
                    state = "Your username and/or password were incorrect."
        else:
            form = LoginForm()
            return render(request, 'login/auth.html', {'state':state, 'form': form})