簡體   English   中英

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

[英]Python Flask: How to use '{}'.format() in a multiline string (""") especially a html one?

您好,目前我知道可以使用 '{}'.format(x) 格式化字符串變量,但是在多行字符串(在這種情況下為長 html 頁面字符串)中,您如何做到這一點?因為如果我將這些 '{}.format()' 放在像這樣的多行字符串中,那么它肯定會以字符串形式返回。

因為這就是問題所在,我只需要為 email 主體制作 html_string,但是必須在此處輸入一些變量,例如下載鏈接。 更不用說取決於是否存在布爾值的特殊情況。


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

output:

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

我個人更喜歡使用 f-strings:

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

output:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM