简体   繁体   English

Django 自动注销和页面重定向

[英]Django auto logout and page redirection

I have an application whose backend is written using django and python.我有一个应用程序,其后端是使用 django 和 python 编写的。 I want to implement auto logout feature in my applcation.我想在我的应用程序中实现自动注销功能。 For this , I used following django built-in features :为此,我使用了以下 django 内置功能:

SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 40
SESSION_SAVE_EVERY_REQUEST = True

I have also used LOGOUT_REDIRECT_URL to redirect the page after logout.我还使用LOGOUT_REDIRECT_URL在注销后重定向页面。

But this doesn't help.但这没有帮助。

  1. After 40 seconds, logout happens but it is not visible in frontend, login page comes only if the user refreshes the tab or makes any request in the tab. 40 秒后,注销发生但在前端不可见,只有当用户刷新选项卡或在选项卡中提出任何请求时,登录页面才会出现。

  2. If I close the browser and open it again , the home page appears with no data as the data are user specific .如果我关闭浏览器并再次打开它,主页将显示没有数据,因为数据是特定于用户的。 and if refresh is done of tab is done , the tab is redirected to login page.如果选项卡已完成刷新,选项卡将重定向到登录页面。

I want to achieve 2 scenarios:我想实现两个场景:

  1. If the auto log happens the user should be automatically redirected to login page.如果自动登录发生,用户应该被自动重定向到登录页面。

  2. If the user closes the browser window and opens it again then user should be displayed the login page of the application.如果用户关闭浏览器窗口并再次打开它,则应向用户显示应用程序的登录页面。

Any suggestions on how to achieve these scenarios ?关于如何实现这些场景的任何建议?

Any help would be appreciated.任何帮助,将不胜感激。 Thank you.谢谢你。

  1. After 40 seconds, logout happens but it is not visible in frontend, login page comes only if the user refreshes the tab or makes any request in the tab. 40秒后,将注销,但在前端不可见,仅当用户刷新选项卡或在选项卡中发出任何请求时,登录页面才会出现。

Yes, that is how it works. 是的,就是这样。 Your browser won't do anything automatically. 您的浏览器不会自动执行任何操作。 You'll have to write necessary Javascript code to monitor the age of the session cookie. 您必须编写必要的Javascript代码来监视会话cookie的寿命。 And when it expires, your Javascript code will load the login page. 并且当它过期时,您的Javascript代码将加载登录页面。

  1. If I close the browser and open it again , the home page appears with no data as the data are user specific . 如果我关闭浏览器并再次打开它,则主页将显示无数据,因为这些数据是用户特定的。 and if refresh is done of tab is done , the tab is redirected to login page. 如果选项卡的刷新完成,则选项卡将重定向到登录页面。

That doesn't sound right. 听起来不对。 If you re-open your browser and visit your app's homepage, it should take you to login page. 如果您重新打开浏览器并访问应用程序的主页,则应该进入登录页面。 But are you trying to restore your browser's session after re-opening it ( Ctrl + Shift + T )? 但是,您是否要在重新打开浏览器的会话( Ctrl + Shift + T )后恢复它?

Auto logout.自动注销。

Add the below two values in settings.py file:在 settings.py 文件中添加以下两个值:

1. SESSION_COOKIE_AGE = TIME #//change expired session 1. SESSION_COOKIE_AGE = TIME #//change expired session

Example: SESSION_COOKIE_AGE = 240*60   #//four hours  or your time
  1. SESSION_SAVE_EVERY_REQUEST = True

############################################################ ############################################### ###########

And then add the below jquery code in your base.html file:然后在 base.html 文件中添加以下 jquery 代码:

    function autorefresh() {

            // auto refresh page after 1 second

            setInterval('refreshPage()', 6*1000);

    }


    function refreshPage() {

            $.ajax({

            url: window.location.pathname,

            success: function(data) {

                window.location = "."

            // $('#console').html(data);

            }

        });

    }

</script>

<script>autorefresh()</script>

<div id="console" ></div>

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

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