简体   繁体   English

Django每次成功登录都会运行一次代码

[英]Django run code once on every successful login

How do I run a piece of code on django after a successful user login? 成功登录用户后,如何在django上运行一段代码? I cannot put the code in the view that is shown after login for the following reasing: 我无法将代码放在登录后显示的视图中以进行以下reasing:

  • The redirect is dynamic, so the view after login differs. 重定向是动态的,因此登录后的视图不同。 It depends on the page the user tried to access. 这取决于用户尝试访问的页面。
  • I want to run my code only once, only right after the moment a user logs in. 我只想在用户登录后立即运行我的代码一次。

I am looking for the most simple pythonic way to do this? 我正在寻找最简单的pythonic方法来做到这一点? Is it possible without writing my own authentication middleware? 没有编写自己的身份验证中间件是否可能?

I'm not sure why you can't put it in the view that processes the login itself. 我不确定为什么你不能把它放在处理登录本身的视图中。 You've certainly written something that takes the output from the username/password form and calls auth.login(), so that would seem to be the right place to do it. 你肯定写过一些从用户名/密码表单输出并调用auth.login()的东西,所以这似乎是正确的地方。

Alternatively, you could write a signal listener for the user_logged_in signal. 或者,您可以为user_logged_in信号编写信号侦听器。

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

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