I have a view that is being called with ajax and returns JSON.
I’d like it to require authentication.
@login_required
def my_view(request):
data = some_data
return HttpResponse(json.dumps(data), mimetype='application/json')
Ideally, if I’m not authenticated this view should return a HTTP status 401 Unauthorized or something similar, so my javascript can correctly interpret the response.
Is it a good practice for requiring authentication on Ajax requests ?
If yes, how could I get this HTTP status from the @login_required decorator ?
login_requireddecorator, you can simply check the credentials insidemy_viewinstead. And if check fails, return 401.