如何在loader.render_to_string内部发送变量以在html中显示它

时间:2018-10-17 09:17:49

标签: python django

我有一个发送电子邮件的功能,我想发送一个要显示在HTML电子邮件模板文件中的变量: 我的django视图:

     id=2
     html = loader.render_to_string(template,id)

     part2 = MIMEText(html, 'html')
     msg.attach(part2)

1 个答案:

答案 0 :(得分:2)

render_to_string接受context作为第二个参数。像这样使用它:

html = loader.render_to_string(template, {'id': id})