12

我正在使用 django-registration,一切都很好,确认电子邮件以纯文本形式发送,但我知道我已修复并以 html 形式发送,但我有一个垃圾问题...... html 代码显示:

<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a>

而且我不需要像...一样显示html代码

任何的想法?

谢谢

4

4 回答 4

27

为了避免修补 django-registration,您应该使用proxy=True扩展 RegistrationProfile 模型:

模型.py

class HtmlRegistrationProfile(RegistrationProfile):
    class Meta:
        proxy = True
    def send_activation_email(self, site):
        """Send the activation mail"""
        from django.core.mail import EmailMultiAlternatives
        from django.template.loader import render_to_string

        ctx_dict = {'activation_key': self.activation_key,
                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                    'site': site}
        subject = render_to_string('registration/activation_email_subject.txt',
                                   ctx_dict)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        message_text = render_to_string('registration/activation_email.txt', ctx_dict)
        message_html = render_to_string('registration/activation_email.html', ctx_dict)

        msg = EmailMultiAlternatives(subject, message_text, settings.DEFAULT_FROM_EMAIL, [self.user.email])
        msg.attach_alternative(message_html, "text/html")
        msg.send()

在您的注册后端,只需使用HtmlRegistrationProfile而不是RegistrationProfile

于 2011-02-26T18:32:00.003 回答
15

我建议同时发送文本版本和 html 版本。在 django-registration 的 models.py 中查找:

send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])

而是从文档http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
于 2009-09-12T05:52:05.510 回答
2

我知道这是旧的并且不再维护注册包。以防万一有人仍然想要这个。@bpierre 答案的附加步骤是:
- 子类 RegistrationView,即您的应用程序的 views.py

class MyRegistrationView(RegistrationView):
...
def register(self, request, **cleaned_data):
    ...
    new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site)

- 在您的 urls.py 中将视图更改为子类视图,即 - 列表项

url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'
于 2014-12-08T00:04:24.390 回答
0

这个人已经扩展了 defaultBackend,使我们能够添加激活电子邮件的 HTML 版本。

具体来说,替代版本工作在这里完成

我成功地使用了后端部分

于 2012-11-22T14:48:39.690 回答