开发者

Django: Password reset email subject line contains 'example.com

I'm using the generic view django.contrib.auth.views.password_reset for the password reset form. In principle, it all works, except that the subject line of the email that's sent out contains 'example.com', as in: "Password reset on example.com".

So I have looked around, but for the life of me I cannot find out how I can change this to contain my actual domain name.

Any ide开发者_StackOverflowas?


The PasswordResetForm sends the email based on your contrib.sites. It gets the domain name to use and passes it to the html template at registration/password_reset_email.html

django/trunk/django/contrib/auth/forms.py:

...
4     from django.contrib.sites.models import get_current_site
...

123     def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
124              use_https=False, token_generator=default_token_generator, from_email=None, request=None):
125         """
126         Generates a one-use only link for resetting password and sends to the user
127         """
128         from django.core.mail import send_mail
129         for user in self.users_cache:
130             if not domain_override:
131                 current_site = get_current_site(request)
132                 site_name = current_site.name
133                 domain = current_site.domain
134             else:
135                 site_name = domain = domain_override
136             t = loader.get_template(email_template_name)
137             c = {
138                 'email': user.email,
139                 'domain': domain,
140                 'site_name': site_name,
141                 'uid': int_to_base36(user.id),
142                 'user': user,
143                 'token': token_generator.make_token(user),
144                 'protocol': use_https and 'https' or 'http',
145             }
146             send_mail(_("Password reset on %s") % site_name,
147                 t.render(Context(c)), from_email, [user.email])

use admin or django shell to change the site

read more about the sites framework here.

How Django uses the sites framework

Although it's not required that you use the sites framework, it's strongly encouraged, because Django takes advantage of it in a few places. Even if your Django installation is powering only a single site, you should take the two seconds to create the site object with your domain and name, and point to its ID in your SITE_ID setting.

in shell you can do this by doing:

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

in your settings.py:

SITE_ID = 2

or

>>> my_site = Site.objects.get(pk=1)
>>> my_site.domain = 'somedomain.com'
>>> my_site.name = 'Some Domain'
>>> my_site.save()

in your settings.py:

SITE_ID = 1


Assuming you have the admin site up go to the "sites" group and change the first one there to your domain?

Either that or there is something in settings.py. http://docs.djangoproject.com/en/dev/topics/settings/#the-basics

I'll just check and find out for you

EDIT:

I am fairly certain thats what I did to make it work for me.


After going through so many answers, I directly digged into base code. Then I found a simple way to configure that.

step 1: First verify that whether you are having password_reset_subject.txt file or not. If not create password_reset_subect.txt & copy below code and paste it there .

{% load i18n %}{% autoescape off %}
{% blocktranslate %}Password reset{{ site_name }}{% endblocktranslate %}
{% endautoescape %}

step 2: Now You need to override subject_template_name in save method. class ExamplePasswordResetSerializer(PasswordResetSerializer):

def save(self):
    request = self.context.get('request')
    opts = {
        'use_https': request.is_secure(),
        'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
        '**subject_template_name':'registration/password_reset_subject.txt**',#it should be your file path.
        'request': request,
        'html_email_template_name':'registration/password_reset_email.html',
    }
    opts.update(self.get_email_options())
    self.reset_form.save(**opts) 


It is simple just go to http://127.0.0.1:8000/admin/sites/site/ if it is in localhost delete example.com and click add site domain = 127.0.0.1 name = 127.0.0.1

go project settings and change SITE_ID = 2


Automated way

You can manually change this as per accepted answer. However if you want to automate this, django documentation recommends using migrations, see Enabling the sites framework, second paragraph.

This migration can be created in existing app, or you can create app specially for this purpose:

#remember to add your_app_name to settings.py, to enable the app
python manage.py startapp your_app_name

Now create empty migration like this:

python manage.py makemigrations --empty your_app_name

This will create migration file 'skeleton'. You will need to fill it out. For example:

from django.contrib.sites.models import Site
from django.db import migrations


def configure_sites_framework(apps, schema_editor):
    Site.objects.create(domain='my-domain-name.com', name='my-domain-name.com')

class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('sites', '__latest__'),
    ]

    operations = [
        migrations.RunPython(configure_sites_framework),
    ]

Of course our app doesn't have to have a model or anything. It depends on sites app migrations, which makes sure your special migration will be run when sites table already exists.

Note, the value is created, not changed. This is because at this stage there is no value yet. But if you check after running migration, this will be the only value in the DB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜