I want to add some Ajax-niceness to my Django-coded website.
In my Django code, I use the @login_required decorator from django.contrib.auth.deco
Built off of Eric Walker's solution, but for Django 2.0
# Standard Imports
import functools
import django.http
def ajax_login_required(view_func):
@functools.wraps(view_func)
def wrapper(request, *args, **kwargs):
if request.user.is_authenticated:
return view_func(request, *args, **kwargs)
return django.http.JsonResponse('Unauthorized', status=401, safe=False)
return wrapper