Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django password reset email subject

Tags:

python

django

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.

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?

Thanks.

like image 772
GivP Avatar asked Dec 10 '11 13:12

GivP


2 Answers

Development Version

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

See https://github.com/django/django/blob/master/django/contrib/auth/templates/registration/password_reset_subject.txt

and https://github.com/django/django/blob/master/django/contrib/auth/forms.py line 150

In Django 1.3

if you use internalization just add in .po file

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

if not folow next steps

in root urls.py

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

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):

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])
like image 197
Alexey Savanovich Avatar answered Sep 23 '22 20:09

Alexey Savanovich


This has been fixed about 8 months ago but the change doesn't seem to have made merged into 1.3.1. See: https://github.com/django/django/commits/master/django/contrib/auth/templates

like image 40
pseudosudo Avatar answered Sep 20 '22 20:09

pseudosudo