Created
November 25, 2016 12:41
-
-
Save agusmakmun/5f04885fa172f6ae8c785cb17e408de5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth import ( | |
authenticate, get_backends, login, logout | |
) | |
#etc.... | |
def userLogin(request): | |
next_page = request.GET.get('next') | |
if request.user.is_authenticated(): | |
if not next_page == None: | |
return HttpResponseRedirect(next_page) | |
else: | |
return redirect('homepage') | |
elif request.method == 'POST': | |
username = request.POST['username'] | |
password = request.POST['password'] | |
""" | |
# Setup remember me | |
# Refference: http://stackoverflow.com/a/32222658/3445802 | |
try: | |
remember = request.POST['remember_me'] | |
if remember: | |
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False | |
except MultiValueDictKeyError: | |
is_private = False | |
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True | |
# Customizing auth backend: http://django.zone/blog/posts/custom-authentication-backends-django/ | |
get_backends() | |
""" | |
user = authenticate(username=username, password=password) | |
if user is not None and user.is_active: | |
login(request, user) | |
if not next_page == None: | |
return HttpResponseRedirect(next_page) | |
else: | |
return redirect('homepage') | |
else: | |
error_message = "Upps, Username and Password didn't match!" | |
return render(request, 'myapp/login.html', {'error_message': error_message}) | |
else: | |
return render(request, 'myapp/login.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment