如何使用Blask Flash闪烁链接闪烁消息?
我正在使用Blask创建一个Web应用程序来处理GoogleOpenid,这些代码几乎完成了,除了闪烁的消息包含一个链接:
@oid.after_login
def create_or_login(resp):
user = db_session.query(User).filter_by(email=resp.email).first()
if user is not None:
flash('Successfully signed in', 'success')
else:
user = User(nickname=resp.fullname, source=GOOGLE, email=resp.email)
db_session.add(user)
db_session.commit()
flash(flashing_message, 'success')
g.user = user
session['nickname'] = user.nickname
return redirect(oid.get_next_url())
当flashing_message这样的情况下,它可以很好地工作:'成功注册,请单击此处'
但是,当flashing_message被'成功注册时,请单击< a href =“/me” class =“ armer-link”> tery</a>'
,它不起作用(什么都没有),而不会丢任何错误。奇怪的是,flash()和返回之间的句子都无法正常工作(未设置会话['nickname]或g.user)。
I'm creating a web app using Flask to deal with GoogleOpenID, these codes are almost finished, except the flashing message contains a link:
@oid.after_login
def create_or_login(resp):
user = db_session.query(User).filter_by(email=resp.email).first()
if user is not None:
flash('Successfully signed in', 'success')
else:
user = User(nickname=resp.fullname, source=GOOGLE, email=resp.email)
db_session.add(user)
db_session.commit()
flash(flashing_message, 'success')
g.user = user
session['nickname'] = user.nickname
return redirect(oid.get_next_url())
It works well when flashing_message is like this: 'Successfully registered, please click here'
But when flashing_message is 'Successfully registered, please click <a href="/me" class="alert-link">here</a>'
, it doesn't work (flashes nothing) without throwing any Error. Strangely, sentences between flash() and return doesn't work either (did not set session['nickname] or g.user).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的其他答案着重于更改模板,以允许将所有闪存消息标记为安全,这可能不是您想要的。
如果您只想将某些闪烁的消息标记为安全,请将传递给flash()的文本包裹在Markup()中。 (“ noreferrer”> for blask api docs for markup
)像这样的标记()中的字符串:
与往常一样,您需要从烧瓶包中导入标记类似:
The other answers here focus on changing your template to allow all flash messages to be marked as safe, which may not be what you want.
If you just want to mark certain flashed messages as safe, wrap the text passed to flash() in Markup(). (Flask API Docs for Markup)
For example, instead of:
Wrap the string in Markup() like this:
As always, you will need to import Markup from the flask package something like:
您需要在调用
flash()
后渲染模板,然后使用get_flashed_messages()
来获取消息。调用flash()后,您需要调整代码以调用模板。 Flash将消息发送到下一个请求,该请求可以由模板提取。该模板看起来像是这样的:
在您的查看代码中,我将在flash()调用后立即添加
render_template
。像:You need to render a template after calling
flash()
which should then get the message usingget_flashed_messages()
.You need to adjust your code to call a template after calling flash(). flash sends the message to next request which can be extracted by the template. The template can look something like:
In your view code, I would add a
render_template
right after the flash() call. Something like:逃脱的HTML是默认行为,因此将其通过
safe
过滤器以显示HTML未播放:Escaping HTML is the default behavior, so pass it through the
safe
filter to display the HTML unescaped:我不确定它是否正确,但是我解决的方法是通过声明函数中的另一个变量,该变量是HTML的字符串,然后通过
render_template()
函数将其传递。在模板中,它通过
安全
过滤器传递。例如,(大致基于)您提供的代码:
然后,在模板中,我在
get_flashed_messages()
中添加了额外的if语句。I am not sure if it is correct, but the way I solved this was by declaring another variable in the function that was a string of HTML and then passing it through a
render_template()
function.And in the template passed it through the
safe
filter.For example, (roughly based on) the code you have provided:
Then in the template I added an extra if statement inside the
get_flashed_messages()
if: