login required ajax,javascript - Django login_required decorator with AJAX - Stack Overflow

I found one question similar to this, but not sure the solution is the best (it doesn't seem very DRY) and it is quite old so I hope its ok that I ask another one.

I have a django project with various pages that require you to be logged in to view them. It is quite simple to get django to redirect to a login page if you are not logged in but what I would like to do is to bring up a modal login with javascript. Obviously I could go through each link and determine manually whether it required login and assign a class to those links or something but I think there must be a better way.

I was thinking possibly along the lines of replacing the built in url template tag with my own that checks if the view is login required (not sure if this is possible) and then if it is, replaces the url with something that can be recognised by javascript to assign a click handler to bring up the modal. Ideal solutions would somehow allow for people without javascript to still be redirected to the login page.

Am I asking too much?

UPDATE

Ok, have come up with some basic code so I'm hoping that might encourage someone to point out where I am going wrong/right. I think that you probably can't check whether a view is login_required with a template tag and so perhaps a decorator for the views is the best option.

#decorators.py

def AJAX_login_required(view_func):

@wraps(view_func)

def wrapper(request, *args, **kwargs):

if request.is_ajax():

if not request.user.is_authenticated():

return HttpResponse(json.dumps({'login_required': True}), mimetype='application/json')

else:

if request.user.is_authenticated():

return view_func(request, *args, **kwargs)

else:

path = request.get_full_path()

login_url = reverse('login')

return redirect_to_login(path, login_url, 'next')

return wrapper

#views.py

@AJAX_login_required

def view_which_requires_login(request):

//etc.

def view_that_does_not(request):

//etc.

#javascript

$('a').click(function(e) {

var link = $(this).attr('href');

$.ajax({

type:"POST",

url:link,

data:'',

success: function(response){

if (response.login_required) {

//open modal login

return;

}

else {

document.location.href = link;

}

},

error: function(){

document.location.href = link;

}

});

});

I think this should result in what I want but it does require that every page that doesn't require login be effectively sent twice. Once to the ajax query and then again when the page is redirected. Is there a better way?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值