烧瓶蓝图在会议上放点东西 [英] Flask Blueprint Putting something on session

查看:48
本文介绍了烧瓶蓝图在会议上放点东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图放

session['logged_in'] = True

在会话中,但是在另一个蓝图中却没有持久...为什么?

in the session, but in another blueprint it doesn't persist... Why is that?

还有什么更好的方法可以保留会话中的内容吗?

Is there any better way to keep something in the session?

扩展:

我有一个蓝图,提供了登录表单.完成并提交后,它将像上面一样设置会话密钥.然后通过

I have a blueprint giving a form to login. When done and submitted, it will set a session key like above. Then it redirects via

返回重定向(url_for('admin.index'))

return redirect(url_for('admin.index'))

进入管理页面,如果我通过

to admin page where If I call the key via

session.get('logged_in')

session.get('logged_in')

我得到无",而不是对或错.

I get "None" Instead of the True or False one.

推荐答案

我想我现在理解您的困惑〜

I think I understand your confusion now~

您的flask会话不会在服务器上存储任何内容.会话"字典将由来自客户端请求的Cookie填充.

Your flask session won't store anything on the server. the 'session' dict is filled by the cookies from the client request.

再次.即:

客户端向服务器发出登录请求,并获得[登录成功]响应以及[cookies],其中包含!!! sessionINFO !!!您认为存储在服务器端.

client make login request to server, and got a [login success] response as well as a [cookies] which contains the !!!sessionINFO!!! you think are stored on the server side.

下次,您必须将整个cookie重新发送到服务器,然后您在服务器中的会话中可能会有数据.

Next time, you must send the whole cookies to the server again, then your session in the server may have data.

浏览器将为您执行此操作.如果您使用本地客户端,请说python请求库.然后,请确保您正在使用会话发出请求(对于requests-lib,它是requests.Session())

Browser will do this for you. If you use a local client, say python requests library. Then be sure you are making requests with session (for requests-lib, it's requests.Session())

------------------ OLD -------------------------------------

------------------OLD-------------------------------------

虽然不是专家,但您描述的情况应该没有发生.

Though not an expert, but the case you described should not have happened.

如果您浏览了Beqa提到的文档,则会话是使用秘密加密的cookie数据.

The session is cookies data encrypted with a secret, if you have gone through the document mentioned by Beqa.

只需设置

app.secret = '........'

并使用会话作为字典.

仅供参考,

客户端请求->服务器(将your_data'logged_in'和client_relating_data'可能是ip,主机等加密,然后将加密后的信息放入cookie'session = ....'中)----->客户端(获取Cookie的响应)

client request---->server (encrypt your_data 'logged_in' and client_relating_data 'maybe: ip, host or etc.', and put the encrypted info in cookies 'session=....') ------> client (get response with cookies)

再次请求客户端----->服务器(用您的机密解密cookie'session = ...'),找到'logged_in'数据并知道您已登录.)

client request again -----> server (decrypt the cookie 'session=...' with your secret), find the 'logged_in' data and know you are logged in.)

cookie如下所示.

the cookies is something like below.

因此,我不确定使用会话时实际上会遇到什么麻烦,并在此处添加一些基本信息.只是希望它能以防万一.

So, I'm not sure what's actually your trouble when using session, and put some basic information here. Just hope it helps in case.

这篇关于烧瓶蓝图在会议上放点东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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