简体   繁体   English

从Flask中格式化html中的字符串

[英]Formatting string in html from flask

Trying to get the html document be able to take in variables from my flask app, but this part is inside a string. 试图让html文档能够从我的flask应用程序中获取变量,但这部分位于字符串中。 How do I go about this? 我该怎么办?

I tried this: 我尝试了这个:

<iframe 
src="https://embed.spotify.com/?uri=spotify%3Auser%3A{{playerid}}%3Aplaylist%3A{{playlistid}}&theme=white".format() width="1400" height="800" frameborder="0" allowtransparency="true"> 
</iframe>

but doesn't seem to work. 但似乎不起作用。 The {{playlistid}} and {{playerid}} is from the python flask app, {{playlistid}}和{{playerid}}来自python flask应用,

return render_template(
    "player.html", playerid = LogInfo['username'], 
    playlistid=LogInfo['playlist'])

只需传递变量名称:

src="https://embed.spotify.com/?uri=spotify%3Auser%3A{{ playerid }}%3Aplaylist%3A{{ playlistid }}&theme=white">

It is no problem to place Jinja2 expressions or variables inside Strings. 将Jinja2表达式或变量放在字符串中是没有问题的。 In fact this is done very often. 实际上,这经常进行。 Jinja2 is rendered by Python on the server side before the HTML code is interpreted so it doesn't matter where in your HTML document your Jinja2 statement is located. Jinja2是在解释HTML代码之前在服务器端由Python呈现的,因此Jinja2语句在HTML文档中的位置无关紧要。

There is actually nothing wrong with your code except of the format() function at the end of your string. 实际上,除了字符串末尾的format()函数外,您的代码没有任何问题。 No need for that. 没必要。 Have a look at the Jinja2 documentation for further infos. 请查看Jinja2文档以获取更多信息。

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

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