Skip to content

Instantly share code, notes, and snippets.

@ratpik
Created August 23, 2013 11:10
Show Gist options
  • Save ratpik/6318129 to your computer and use it in GitHub Desktop.
Save ratpik/6318129 to your computer and use it in GitHub Desktop.
A simple way to log-in a user via an ajax request
from annoying.decorators import ajax_request
from django.contrib.auth import authenticate, login
from django.views.decorators.http import require_http_methods
@ajax_request
@require_http_methods(["POST"])
def ajax_login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return {"status" : "true"}
else:
return {"status" : "false", "reason" : "You need to activate your account. Please check your email"}
else:
return {"status" : "false", "reason" : "Invalid username/password"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment