Django authentication and Ajax - URLs that require login

前端 未结 5 1383
暗喜
暗喜 2020-11-29 15:45

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

5条回答
  •  春和景丽
    2020-11-29 16:34

    Here is proposed version of the decorator with wrap.__doc__ , wrap.__name__

    from functools import wraps
    
    def ajax_login_required(function):
        def wrap(request, *args, **kwargs):
            if request.user.is_authenticated():
                return function(request, *args, **kwargs)
            json = simplejson.dumps({ 'not_authenticated': True })
            return HttpResponse(json, mimetype='application/json')  
        wrap.__doc__ = function.__doc__
        wrap.__name__ = function.__name__
        return wrap
    

提交回复
热议问题