Django+使用 django-registration 以 html 格式发送电子邮件

发布于 2024-08-03 06:10:56 字数 372 浏览 5 评论 0原文

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

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

并且我不需要显示html 代码就像...

有什么想法吗?

谢谢

im using django-registration, all is fine, the confirmation email was sending in plain text, but know im fixed and is sending in html, but i have a litter problem... the html code is showing:

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

and i dont need to show the html code like the ...

Any idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风为裳 2024-08-10 06:10:56

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

models.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

To avoid patching django-registration, you should extend the RegistrationProfile model with proxy=True:

models.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()

And in your registration backend, just use HtmlRegistrationProfile instead of RegistrationProfile.

暖树树初阳… 2024-08-10 06:10:56

我建议同时发送文本版本和 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', '[email protected]', '[email protected]'
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()

I'd recommend sending both a text version and an html version. Look in the models.py of the django-registration for :

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

and instead do something like from the docs http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
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()
自演自醉 2024-08-10 06:10:56

我知道这是旧的并且不再维护注册包。以防万一有人还想要这个。
回答 @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'),'

I know this is old and the registration package is no longer maintained. Just in case somebody still wants this.
The additional steps wrt to the answer of @bpierre are:
- subclass the RegistrationView, i.e. your app's views.py

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

- in your urls.py change the view to the sub-classed view, i.e.
- List item

url(r'accounts/register/
, MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'
起风了 2024-08-10 06:10:56

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

具体来说,备用版本作业已完成 这里

我成功地使用了后端部分

This guy have extended the defaultBackend enabling us to add an HTML version of the activation email.

Specifically, the alternate version job is done here

I managed to use the backend part successfully

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文