简体   繁体   English

用户在django中登录后如何检查每个页面上的条件

[英]how to check for condition on each page after a user has logged in in django

I have a condition, whether a user has setup some account information or not. 我有一个条件,即用户是否设置了一些帐户信息。 If a user does not meet this condition, I want to throw a popup on the screen. 如果用户不符合此条件,我想在屏幕上弹出一个弹出窗口。

How can I accomplish this in the most resource efficient way, so as to not have to query the database for every view a user hits. 我该如何以最节省资源的方式完成此任务,从而不必为用户点击的每个视图查询数据库。 Or is this not a problem because it is optimized through django? 还是这不是问题,因为它是通过Django优化的?

Look into implementing a decorator for the view you are interested in restricting access to - Django provides some decorators that are useful. 考虑为您希望限制访问的视图实现装饰器-Django提供了一些有用的装饰器 If you do not find one that fits your needs perfectly, consider writing a custom one. 如果找不到适合您需求的产品,请考虑编写自定义产品。

Edit: To reduce the number of database hits, you should save the results in the cache/session. 编辑:为了减少数据库命中的次数,您应该将结果保存在缓存/会话中。

我最终针对我的特定情况使用了上下文处理器,但是对于检查条件,我相信定制中间件是解决方案。

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

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