Search code examples
django-rest-frameworkdjango-allauthdjango-rest-auth

How to customize a URL to reset password in an email template?


I am using Django Rest Auth with Django All Auth.

When I hit an endpoint /rest-auth/password/reset/ (POST) I receive an email includes a URL to reset a password.

But the URL doesn't work, because a domain is always example.com and uid token don't work well like below...

http://example.com/password/reset/confirm/(%3FPMzQ%5B0-9A-Za-z_%5C-%5D+)/(%3FP5ku-afb8832e99157c02f452%5B0-9A-Za-z%5D%7B1,13%7D-%5B0-9A-Za-z%5D%7B1,20%7D)/$

I've overridden a PasswordResetSerializer to use my email template referring to this article

How can I solve this situation?


Here are the codes:

serializers.py

from rest_auth.serializers import PasswordResetSerializer


class CustomPasswordResetSerializer(PasswordResetSerializer):

    def get_email_options(self):
        print("check override")

        return {
            'email_template_name': 'password_reset_key_message.txt',

        }

password_reset_key_message.txt

A password reset has been requested.
Click the link below to continue.

{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

If I change a part of {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} to {{ password_reset_url }} the email doesn't include any URL


When I add 'domain_override' in def get_email_options(self) like berow,

from rest_auth.serializers import PasswordResetSerializer


class CustomPasswordResetSerializer(PasswordResetSerializer):

    def get_email_options(self):
        print("check override")

        return {
            'domain_override': 'localhost:8000',
            'email_template_name': 'password_reset_key_message.txt',

        }

The URL became like below,

http://localhost:8000/password/reset/confirm/(%3FPMzQ%5B0-9A-Za-z_%5C-%5D+)/(%3FP5ku-afb8832e99157c02f452%5B0-9A-Za-z%5D%7B1,13%7D-%5B0-9A-Za-z%5D%7B1,20%7D)/$

In this case, I can specify a domain, but uid and token still don't work...


python: 3.7.5

Django:2.2.2

django-allauth:0.41.0

django-rest-auth:0.9.5

djangorestframework:3.12.1


Solution

  • If you meant to change the example.com to another domain you will need to change the domain on the database. To do that create a superuser by python manage.py createsuperuser and then go to the /admin page. Then click on the change button on the Sites option. And then you will see 'example.com' just click on it and change it to your expected domain. And yes, you don't need the so critical way you specified.

    UPDATE

    The actual answer that worked for the questioner is to do the things mentioned above and also replace path('accounts_api/password/reset/confirm/(..... to re_path(r'^accounts_api/password/reset/confirm/(..... in urls.py