Django password reset email subject

I have successfully added my own HTML templates for reset password pages in Django, and it all works great. The only bit that I cannot decide is how to include my own question via email.

The default password is reset to [my site name], "and I can change the site name in the admin, but does anyone know how to override the entire subject line?

Thanks.

+6
source share
3 answers

Development version

Just create a new registration/password_reset_subject.txt file in the templates directory. This will override the default django object

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 to the .po file

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

if you do not follow the next steps

in urls.py root folder

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

in your file 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 the save method in your_app / forms.py (I know this is 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]) 
+13
source

This was fixed about 8 months ago, but the change did not seem to merge in 1.3.1. See: https://github.com/django/django/commits/master/django/contrib/auth/templates

+1
source

PasswordResetForm sends an email based on contrib.sites . It gets the domain name and passes it to the html template in registration/password_reset_email.html

use admin or django shell to change site

in the shell, you can do this by doing:

 >>> from django.contrib.sites.models import Site >>> my_site = Site(domain='your_domain.com', name='Your Domain') >>> my_site.save() >>> print my_site.id 2 >>> 

in your settings.py:

 SITE_ID = 2 
0
source

Source: https://habr.com/ru/post/903513/


All Articles