简体   繁体   English

将flask会话变量传递到其他视图将返回类型None

[英]Passing a flask session variable to a different view returns type None

I am trying to pass a session variable that is set on the login view, to another view. 我试图将在登录视图上设置的会话变量传递给另一个视图。 However, the session variable always returns None if a form is submitted, I want the session variable to retain. 但是,如果提交了表单,则会话变量始终返回无,我希望保留会话变量。

The tools page has some form fields, and one text box that handles autocomplete search based off of the session['foo'] which is sent on good login. 工具页面具有一些表单字段,以及一个文本框,该文本框基于会话['foo']进行自动完成搜索,该会话在登录成功后发送。

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        # Get some form variables here
        if login is valid:
            login_user(user, remember=True)
            session['foo'] = utils.get_data()
            return redirect(url_for('tools'))
    return render_template('login.html')

@app.route("/tools", methods=['GET', 'POST'])
@login_required
def tools():
    if session['foo'] is None:
        print "Data is None type, getting data"
        session['foo'] = utils.get_data()

    if request.method == 'POST':
        # Handling some forms here
        # If one of these forms are submitted, session['foo'] is set to None :(

    return render_template('tools.html', foo_data=session['foo'])

Utils.py 实用程序

def get_data():
    response = requests.post(url, data=data)  # FYI, server won't allow GET for this call.
    # Do json type stuff here to make response.content nice for javascript
   return response

Essentially, what happens with this code is, every time I submit a form, I have to check if it's None and invoke utils.get_data(), I want the data returned by this method to retain for the entire length of the session. 本质上,这段代码发生的事情是,每次我提交表单时,我都必须检查它是否为None并调用utils.get_data(),我希望此方法返回的数据在会话的整个过程中都保留。

EDIT: Solution, session variable object is too big. 编辑:解决方案,会话变量对象太大。 Some ways to solve this could include; 解决此问题的一些方法可能包括:

  • Use AJAX to avoid refresh the page each submission, thus not flushing the session object 使用AJAX避免每次提交时刷新页面,从而不刷新会话对象
  • Split the object into smaller chunks 将对象分成较小的块
  • Avoid storing large objects in cookies- use local storage 避免将大对象存储在cookie中-使用本地存储

Check the size of the data being put into session. 检查放入会话中的数据的大小。 And if it is too large try using a smaller object. 如果太大,请尝试使用较小的对象。

From the documentation 文档中

A note on cookie-based sessions: Flask will take the values you put into the session object and serialize them into a cookie. 关于基于cookie的会话的说明:Flask将采用您放入会话对象中的值并将其序列化为cookie。 If you are finding some values do not persist across requests, cookies are indeed enabled, and you are not getting a clear error message, check the size of the cookie in your page responses compared to the size supported by web browsers. 如果发现某些值在请求中不存在,则表明确实启用了cookie,并且没有收到明确的错误消息,请检查页面响应中cookie的大小与网络浏览器支持的大小。

Original post: 原始帖子:

Technically the utils.get_data method is a separate session than the web call. 从技术上讲,utils.get_data方法是一个与Web调用不同的会话。 What is the get data method trying to achieve? get data方法试图实现什么?

You can also create a server-side session if the data size is the problem. 如果数据大小有问题,您也可以创建服务器端会话。 It is possible to store Flask sessions inside your database and just store the session id in the cookie. 可以将Flask会话存储在数据库中,而只需将会话ID存储在cookie中即可。

For example this snippet shows how to store your session data in MongoDB. 例如,此代码段显示了如何在MongoDB中存储会话数据。

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

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