I am trying to authenticate user on ajax post but doesn\'t work. Here what I have done
settings.py
LOGIN_URL = \'/accounts/login/\'
LOGI
Instead of :
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login')
return json response :
if request.user.is_authenticated():
## write your code...
jsonr = json.dumps({ 'authenticated': True })
return HttpResponse(jsonr, mimetype='application/json')
else:
jsonr = json.dumps({ 'authenticated': False })
return HttpResponse(jsonr, mimetype='application/json')
And At your ajax success response , if not authenticated then redirect to login using windows.location
.
OR you can write decorator : Django authentication and Ajax - URLs that require login