Django密码重置电子邮件主题 [英] Django password reset email subject

查看:59
本文介绍了Django密码重置电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Django中的密码重置页面成功添加了自己的自定义HTML模板,并且一切正常.我无法确定如何包括我自己的电子邮件主题.

I have successfully added my own custom HTML templates for the password reset pages in Django and it's all working nicely. The only bit I can't work out it how to include my own email subject.

默认值为"[我的网站名称]上的密码重置",我显然可以在admin中更改网站名称,但是有人知道如何覆盖整个主题行吗?

The default is "Password reset on [my site name]" and I can obviously change the site name in admin but does anyone know how to override the whole subject line?

谢谢.

推荐答案

开发版本

只需在模板目录中创建新文件 registration/password_reset_subject.txt .这将覆盖默认的django主题

Just create new file registration/password_reset_subject.txt in your templates dir. This will override default django subject

请参见 https://github.com/django/django/blob/master/django/contrib/auth/templates/registration/password_reset_subject.txt

https://github.com/django/django/blob/master/django/contrib/auth/forms.py 第150行

在Django 1.3中

如果使用内部化,只需添加 .po 文件

if you use internalization just add in .po file

#: forms.py:143
#, python-format
msgid "Password reset on %s"
msgstr "YOUR SUBJECT HERE %s"

如果不遵循后续步骤

在根urls.py

# change to your custom view
(r'^password_reset/$', 'your_app.views.password_reset'),

在your_app/views.py

in your your_app/views.py

from django.contrib.auth.views import password_reset as django_password_reset
from .forms import CustomPasswordResetForm

# reuse Django view, but change form
def password_reset(*args, **kwargs):
    kwargs['password_reset_form'] = CustomPasswordResetForm
    django_password_reset(*args, **kwargs):

在your_app/forms.py中重写保存方法(我知道它不是DRY,但应该可以工作:)

rewrite save method in your your_app/forms.py (I know it not DRY but should work :)

class CustomPasswordResetForm(PasswordResetForm):
    def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
         use_https=False, token_generator=default_token_generator, request=None):
        from django.core.mail import send_mail
        for user in self.users_cache:
            if not domain_override:
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
            else:
                 site_name = domain = domain_override
            t = loader.get_template(email_template_name)
            c = {
            'email': user.email,
            'domain': domain,
            'site_name': site_name,
            'uid': int_to_base36(user.id),
            'user': user,
            'token': token_generator.make_token(user),
            'protocol': use_https and 'https' or 'http',
            }
            send_mail(_("YOUR SUBJECT HERE %s") % site_name,
                t.render(Context(c)), None, [user.email])

这篇关于Django密码重置电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆