【问题标题】:Formatting string in html from flask从烧瓶中格式化html中的字符串
【发布时间】:2017-01-12 02:44:13
【问题描述】:

试图让 html 文档能够从我的烧瓶应用程序中获取变量,但这部分位于字符串中。我该怎么办?

我试过了:

<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>

但似乎不起作用。 {{playlistid}} 和 {{playerid}} 来自 python flask 应用程序,

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

【问题讨论】:

    标签: python html iframe flask spotify


    【解决方案1】:

    只需传递变量名:

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

    【讨论】:

      【解决方案2】:

      在字符串中放置 Jinja2 表达式或变量是没有问题的。事实上,这种情况经常发生。 Jinja2 在解释 HTML 代码之前由服务器端的 Python 呈现,因此 Jinja2 语句在 HTML 文档中的位置无关紧要。

      除了字符串末尾的format() 函数之外,您的代码实际上没有任何问题。没必要。查看Jinja2 documentation 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2018-05-03
        • 2016-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 2017-09-29
        • 1970-01-01
        • 2020-08-20
        相关资源
        最近更新 更多