【问题标题】:Django Contact Form send html emailsDjango 联系表发送 html 电子邮件
【发布时间】:2011-05-30 03:44:57
【问题描述】:

有没有办法使用 django-contact-form 发送 html 电子邮件?

【问题讨论】:

    标签: html django contact-form


    【解决方案1】:

    我只是查了一下源码,不是内置的。

    你当然可以随心所欲地修改它——这是一个非常简单的形式并且非常易读。

    您需要添加一个 html_template 变量/路径并修改保存行,并添加一种方法来指定要使用的模板。

    def save(self, fail_silently=False):
        """
        Build and send the email message.
    
        """
        data = self.get_message_dict()
        msg = EmailMultiAlternatives(subject=data['subject'], body=data['message'], from_email=data['from_email'], to=data['recipient_list'])
    
        msg.attach_alternative(loader.render_to_string('path_to_my_html_template.html', self.get_context()), "text/html")
        msg.send(fail_silently=fail_silently)
    

    这样更容易将所有更改保存在一个位置。

    否则,您可以重写大部分代码并更改get_message_dict() 中的方法名称和字符串以反映EmailMessage 的新字段名称

    坦率地说,我不确定为什么 send_mail 的关键字应该与 EmailMessage 不同

    【讨论】:

    • 我收到一个错误“__init__() got an unexpected keyword argument 'message'”,其中 msg = 代码行和视图“form.save(fail_silently=fail_silently)”的第 73 行
    • 好的,因为 send_mail 将所有参数转换为正确的 EmailMessage 参数,按照这种编写方式,您将不得不更改此处的大部分代码(名称)或使用其他方式获得正确的关键字..更新帖子
    • 完美运行!感谢您的帮助。
    猜你喜欢
    • 2018-04-15
    • 2020-12-07
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2020-05-05
    • 2020-05-20
    • 2019-10-03
    • 2016-03-02
    相关资源
    最近更新 更多