我使用的是Python的字符串製作的HTML電子郵件,像這樣解:Python的ValueError異常:值過多與字符串格式化
# Code setting up the message html
message = "long html message string"
scoped = ""
if settings.DEBUG:
scoped = "scoped"
header = """
<style %s type='text/css'>
@media only screen and (max-width: 480px){
.emailImage{
height:auto !important;
max-width:200px !important;
width: 100% !important;
}
}
</style>
""" % scoped
footer = "html message footer"
message = header + message + footer
# Code sending the message.
的問題是,上面的代碼給我的錯誤ValueError: too many values to unpack
。但是,如果我從消息中刪除scoped
變量,則html將運行,即,這可以工作(雖然不需要將範圍變量添加到我的HTML中)。
# Code setting up the message html
message = "long html message string"
header = """
<style type='text/css'>
@media only screen and (max-width: 480px){
.emailImage{
height:auto !important;
max-width:200px !important;
width: 100% !important;
}
}
</style>
"""
footer = "html message footer"
message = header + message + footer
# Code sending the message.
爲什麼第一個版本拋出該錯誤,我該如何解決ValueError?
你爲什麼要通過字符串替換建立HTML?這是模板的用途。 –