Python Flask:如何在多行字符串(" "")中使用 '{}'.format(),尤其是 html 字符串?



你好,目前我知道可以用"{}".format(x(格式化字符串变量,但在多行字符串(在这种情况下是一个长html页面字符串(中如何精确地做到这一点?因为如果我把这些"{}.format(("放在像这样的多行字符串中,它肯定会以字符串的形式返回。

因为这就是问题所在,我只需要为电子邮件正文生成html_string,但一些变量,如下载链接,必须在这里。更不用说一个特殊的场景,取决于是否有bool。


def generate_html_body(attachment_bool: bool,download_link: str):
if attachment_bool is False:
Letter_1 = "Helaas was deze te groot om via mail door te sturen."
Letter_2 = "Klik hier om het bestand te downloaden."
if attachment_bool is True:
Letter_1 =  "U vindt het bestand terug in de bijlage."
Letter_2 = "Is het bestand niet in de bijlage terug te vinden? Klik dan hier en download dan hier."

html_string="""<html>
<head>
<title>{{ title }}</title>
</head>

<body>
<p>Uw GIPOD data is gereed,</p>
<p> { Letter_1}</p>
<a href="{}"> {{  Letter_2 }} </a>
<p> U heeft 30 dagen de tijd om deze te downloaden. Bedankt om CLASSIFIED te gebruiken. </p>
<table>
<tr>
<td>
<p>
<![if !vml]><img width=500 height=500 src="" alt="logo" class="img-responsive"><![endif]>
</p>
</td>
</tr>
<tr>
<td>
<p></p>
</td>
<td>
</td>
</tr>
<tr>
<td>
<p>
<span></span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<span style='font-size:10.0pt;line-height:105%;font-family:Roboto;color:#00273F'>
classiefie<o:p></o:p>
</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<b>
<span>
<a href="classiefie"">
<span style='color:#0563C1'>classiefie"/span>
</a>
</span>
</b><span></span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<span></span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<b>
<span lang=NL style='font-size:7.0pt;line-height:105%;font-family:Roboto;
color:#5EBA00;'>
Gelieve rekening te houden met het milieu
voordat u dit document afdrukt<o:p></o:p>
</span>
</b>
</p>
</td>
</tr>
<tr>
<td>
<p>
<span></span>
</p>
</td>
</tr>
<tr>
<td>
<p>
<span>
<![if !vml]><img img width=500 height=500 src="classiefie">
</span>
</p>
</td>
</tr>
</table>
</body>
</html>"""

str.format()适用于多行字符串,我在Flask应用程序中将其用于HTML,没有任何问题。

print("""
Here's the number I want to show you: {number}
""".format(number=42)
)

输出:

Here's the number I want to show you: 42

我个人更喜欢使用f-string:

number = 42
print(f"""
Here's the number I want to show you: {number}
"""
)

输出:

Here's the number I want to show you: 42

最新更新