如何使用Flask跟踪匿名用户 [英] How to track anonymous users with Flask

查看:169
本文介绍了如何使用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_userAnonymousUserMixin对象.假设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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆