简体   繁体   English

如果用户已注销,则对django @login_required视图的Ajax调用将返回登录页面

[英]Ajax call to django @login_required view returns login page if user had logged out

If wrap a view function in @login_required and call it using $.ajax({...}) and the user has logged out (from another tab for example), the success() call back is called and the data is the content of the login page redirect. 如果将视图函数包装在@login_required中,并使用$ .ajax({...})进行调用,并且用户已注销(例如,从另一个选项卡退出),则会调用success()回调,并且数据即为内容登录页面重定向。

Is there a way for the @login_required decorator to return an error code if it's an ajax request rather than a redirect, so I can correctly redirect in javascript? @login_required装饰器是否有一种方法可以返回错误代码(如果它是ajax请求而不是重定向),那么我可以在javascript中正确重定向吗?

This post ( What's the best way to handle session timeouts in ajax requests? ) indicated I would have to write my on @login_required decorator... Isn't there a way to handle this within the core django? 这篇文章( 处理ajax请求中的会话超时的最佳方法是什么? )表明我将不得不在@login_required装饰器上编写我的代码……在django核心中没有解决此问题的方法吗?

Check out the permission_required decorator. 签出Permission_required装饰器。 It can be set to return a 403 error code instead of a login page redirect. 可以将其设置为返回403错误代码,而不是返回登录页面重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM