Flask消息在重定向之间闪烁失败 [英] Flask message flashing fails across redirects

查看:198
本文介绍了Flask消息在重定向之间闪烁失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Flask Google App Engine 。调用 get_flashed_messages()在我刷新消息时返回空白,然后使用 redirect()



$ $ p $ @ views.route('/ todo / add',methods = [POST])
def add_todo():
flash('hey')
return redirect(url_for('todo_list'))

但是,如果我注释掉

 #SERVER_NAME ='localhost'

那么它似乎工作正常。我的问题是,我必须使用子域名,所以我需要设置SERVER_NAME。



交易是什么?

解决方案

我懂了!
窍门是将服务器名称设置为带点的东西。

因此'localhost'变成了'app.local',app.local应该被添加到/ etc / hosts,指向与localhost相同的地址。



docs
$ b


请记住,不仅
Flask存在不知道
子域名的问题,您的网页浏览器
也是如此。大多数现代网络浏览器
将不允许在没有
点的服务器名称上设置跨子域cookie
。因此,如果您的服务器名称是
'localhost',您将无法
为localhost和
的每个子域设置一个cookie。请在这种情况下选择一个
不同的服务器名称,
像'myapplication.local',并添加
这个名字+你想要
的子域名使用到你的主机配置或设置
本地绑定。



I'm currently working on a project using Flask and Google App Engine. Calling get_flashed_messages() returns empty when I flash a message then use a redirect():

@views.route('/todo/add', methods=["POST"])
def add_todo():
    flash('hey')
    return redirect(url_for('todo_list')) 

However, if I comment out

# SERVER_NAME = 'localhost'

then it seems to work fine. My problem is that I have to use subdomains so I need SERVER_NAME to be set.

What is the deal?

解决方案

I got it! The trick is to set server name to something with dots.

So 'localhost' became 'app.local' and app.local should be added to /etc/hosts, pointing to the same address as localhost.

From the docs:

Please keep in mind that not only Flask has the problem of not knowing what subdomains are, your web browser does as well. Most modern web browsers will not allow cross-subdomain cookies to be set on a server name without dots in it. So if your server name is 'localhost' you will not be able to set a cookie for 'localhost' and every subdomain of it. Please chose a different server name in that case, like 'myapplication.local' and add this name + the subdomains you want to use into your host config or setup a local bind.

这篇关于Flask消息在重定向之间闪烁失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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