问题描述
我正在编写一个Web应用程序,该应用程序在用户登录时(并且只在用户登录时)从第三方服务器接收大量数据。将这些数据解析为自定义对象并存储在列表中()
。现在,用户可以在应用程序中使用这些数据,调用不同的视图(例如发送不同的请求)。
我不确定什么是最好的模式来传递视图函数之间的对象列表
I am writing a web application that receives a lot of data from a third party server, when (and only when) a user logs in. This data is parsed to custom objects and stored in a list()
. Now the user works with this data all around the application, calling different views (e.g. sending different requests).I'm not sure what's the best pattern to pass the list of objects between the view functions?
我技术上看到两个可能性,但在我的情况下都有缺点:$ b
I technically see two possibilities, but both have drawbacks in my case:
- 会话字典:在会话中存储数据(整个列表将在每个请求中在服务器和浏览器之间来回传送)
- 坚持:将数据暂时保存到数据库似乎更加充分。但是我希望根本不需要使用数据库(除了这个暂时的数据,我没有任何需要存储在本地的数据,其他的一切都是从第三方服务器接收到的,并发回给它) / li>
- The session dict: Storing the data in the session is an overkill (the whole list would be send back and forth between server and browser on every request)
- Persisting: Temporarly persisting the data to a database seem more adequate. But I was hoping to not having to use a database at all (except for this temporarly data I don't have any data that needs to be stored locally. Everything else is received from the third party server and sent back to it).
我不是一个非常有经验的Web开发人员,所以也许我会监督这个显而易见的事情。
那么还有另外一种方法可以在请求之间传递数据吗?也许有一些内置的魔法或坚持(到一个文件或数据库)真的唯一的选择?
I'm not a very expirienced web developer, so maybe I oversee the obvious.So is there another way to pass the data between requests? Maybe some built in flask magic or is persisting (to a file or database) really the only option?
推荐答案
虽然Flask的默认实现对于会话是将数据存储在一个cookie,这不是唯一的方法来做到这一点。通常,您在Cookie中存储会话ID,数据本身存储在服务器的某个位置,并通过该cookie进行检索。
Although Flask's default implementation for sessions is to store the data in a cookie, that's not the only way to do it. Typically, you store a session ID in the cookie, and the data itself is stored somewhere on the server and retrieved via that cookie.
Flask为您提供了一个简单的方法覆盖默认的会话实现,并有各种各样的食谱周围显示如何做到这一点 - 这里是一个SO问题,显示大纲。
Flask does provide you with an easy way of overriding the default session implementation, and there are various recipes around showing how to do that - here's an SO question that shows the outline.
这篇关于如何在Flask中的视图函数/会话中传递复杂的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!