I'm creating a project (an api), but I'm stuck on the next part.
When sending the password reset mail, specifically password_reset_key_message.txt
, I can't capture the user's 'key' and 'uid', I want to change the address.
Email delivery work fine, my problem is with password_reset_key_message.txt
.
Packages
django==4.0.7
dj-rest-auth==2.2.5
django-allauth==0.51.0
I'm looking for something like this:
{% extends "account/email/base_message.txt" %}
{% load i18n %}
{% block content %}{% autoescape off %}{% blocktrans %}You're receiving this e-mail because you or someone else has requested a password for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to reset your password.{% endblocktrans %}
---
https://fontend-project.com/password-reset?id={{ uid }}&key={{ key }}
---
{% if username %}
{% blocktrans %}In case you forgot, your username is {{ username }}.{% endblocktrans %}{% endif %}{% endautoescape %}{% endblock %}
But i can't capture 'uid and key.
By default it uses '{{ password_reset_url }}', but I want to change the address, I need 'uid' and 'key' which are provided in '{{ password_reset_url }}' and I can't capture them, which I can do in the template o message file 'email_confirmation_message.txt'.
Help please, I have tried and could not find the solution.
To resolve this issue, I did the following:
settings.py
of the main project, add a custom_password_serializer:config/settings.py
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER': 'myapp.serializers.CustomPasswordResetSerializer'
}
config/serializers.py
from dj_rest_auth.serializers import PasswordResetSerializer
from myapp.forms import CustomResetForm
class CustomPasswordResetSerializer(PasswordResetSerializer):
"""
Serializer for requesting a password reset e-mail.
"""
@property
def password_reset_form_class(self):
return CustomResetForm
forms.py
config/forms.py
from dj_rest_auth.forms import AllAuthPasswordResetForm
from django.contrib.sites.shortcuts import get_current_site
from allauth.account.forms import default_token_generator
from allauth.account.adapter import get_adapter
from allauth.account.utils import user_pk_to_url_str
class CustomResetForm(AllAuthPasswordResetForm):
def save(self, request, **kwargs):
current_site = get_current_site(request)
email = self.cleaned_data['email']
token_generator = kwargs.get('token_generator', default_token_generator)
for user in self.users:
temp_key = token_generator.make_token(user)
uid = user_pk_to_url_str(user)
context = {
'current_site': current_site,
'user': user,
'key': temp_key,
'uid': uid,
}
get_adapter(request).send_mail(
'account/email/password_reset_key', email, context
)
return self.cleaned_data['email']
password_reset_key_message.txt
fileRemember that password_reset_key_message.txt
must go in the following path backend_directory/api/templates/account/email/
Thanks to StephenSorriaux for this solution, Source.
I hope this solution helps.