我使用Python字符串为电子邮件创建html,如下所示:
# 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呢?
发布于 2015-06-11 21:04:24
在width元素后面有一个未转义的%符号,添加另一个%来转义它:
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请注意,当您摆脱% scoped时,您不再格式化字符串,%字符也不再特殊。
https://stackoverflow.com/questions/30791377
复制相似问题