如何使用Flask跟踪匿名用户 [英] How to track anonymous users with Flask
问题描述
我的应用程序实现了一个购物车,匿名用户可以在其中使用产品填充他们的购物车.仅在付款前需要用户登录.如何实现?
My app implements a shopping cart in which anonymous users can fill their cart with products. User Login is required only before payment. How can this be implemented?
主要挑战是烧瓶必须跟踪用户(即使是匿名用户)及其订单.我当前的方法是利用分配给current_user
的AnonymousUserMixin
对象.假设current_user
在整个会话期间都不会改变.但是,我注意到,例如,在每次浏览器页面刷新时,都会向current_user
分配一个新的AnonymousUserMixin
对象.请注意,如果对用户进行身份验证,则不会发生这种情况.
The main challenge is that flask must keep track of the user (even if anonymous) and their orders. My current approach is to leverage the AnonymousUserMixin
object that is assigned to current_user
. The assumption is that current_user
will not change throughout the session. However, I noticed that a new AnonymousUserMixin
object is assigned to current_user
, for example, upon every browser page refresh. Notice that this does not happen if a user is authenticated.
关于如何规避此事的任何建议?
Any suggestions on how to circumvent this?
推荐答案
不需要自定义AnonymousUserMixin
,您可以将购物车数据保留在会话中:
There is no need for a custom AnonymousUserMixin
, you can keep the shopping cart data in session:
- 匿名用户向购物车添加了一些内容->使用购物车数据更新了会话
- 用户要签出->将他重定向到登录页面
- 已登录的用户又回到了结帐页面->将其购物车数据从会话中取出,并进行所有操作(如果他一直都在登录状态)
这篇关于如何使用Flask跟踪匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!