-
Notifications
You must be signed in to change notification settings - Fork 308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to send customized email template for password reset endpoint? #9
Comments
This is how I do it on my projects (DRF API Server + React SPA or Next.js): Declare a custom serializer for registration that inherits from the base. Then you have complete control over what is included in the email. from dj_rest_auth.registration.serializers import RegisterSerializer
class CustomRegistrationSerializer(RegisterSerializer):
def save(self, request):
# handle all setup logic and email here. then, in your Django settings: REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'path.to.your.CustomRegistrationSerializer'
} |
I will try it and get back to you, thanks |
I was able to do it similarly as you suggested. Create your custom password reset serializer from dj_rest_auth.serializers import PasswordResetSerializer
class CustomPasswordResetSerializer(PasswordResetSerializer):
def save(self):
request = self.context.get('request')
# Set some values to trigger the send_email method.
opts = {
'use_https': request.is_secure(),
'from_email': 'example@yourdomain.com',
'request': request,
# here I have set my desired template to be used
# don't forget to add your templates directory in settings to be found
'email_template_name': 'password_reset_email.html'
}
opts.update(self.get_email_options())
self.reset_form.save(**opts) UPDATE: If you only want to customize email parameters nothing more or less, you can ignore overriding Ex: from dj_rest_auth.serializers import PasswordResetSerializer
class MyPasswordResetSerializer(PasswordResetSerializer):
def get_email_options(self) :
return {
'email_template_name': 'password_reset_email.html'
}
then point to your custom serializer in settings.py REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER': 'path.to.your.CustomPasswordResetSerializer'
} |
@mohmyo did you create your own base class for PasswordResetSerializer? I only see RegisterSerializer in dj_rest_auth.registration.serializers |
@aaronamelgar Nope, I have updated my above code snippet regarding the base class, check it out. |
please help me for password reset, its giving error as following NoReverseMatch at /dj-rest-auth/password/reset/ |
This has been answered many times, see closed issues and you will find so many helpful discussions with the solution |
Like this |
How to do the same (Edit the email template) for email sent to verify the email after registration? |
This part is controlled by |
Thanks, it is done 😎. If anyone having trouble getting the key sent at the end of the url you can use {{ key }} in that template to access the key and later goto the verify email address and post the key by frontend. |
@mohmyo How does your template look like? Where do you exactly put a custom URL pointing to the frontend? |
@colonder can you be more specific? |
@mohmyo I have followed this and the template variables such as Email I receive looks like this
Am I missing something?? |
As |
After some digging, I found that the password reset email template is coming from Overwriting Although, to send the HTML content you will have to write a custom password reset serializer as @mohmyo suggested and replace this @mohmyo Thanks for your Solution;) |
You are welcome @Abishek05, glad it helped. |
Hello @mohmyo and @Abishek05 , even after doing it with your approach I'm getting error which is I'm contributing to an open-source org, Now let me tell you about the structure of the org, basically serializer.py
settings.py file
Will you please help me with what to do in this situation, Also if I need to do the complete process by overwriting how would I do that because here we have templates for each app so we cannot define the TEMPLATE_DIR in the settings file? I've been stuck to this issue for the past 4-5 days, please help me out. |
Hello @lkbhitesh07, is there a better error trace than the one you posted? I'm not sure where to look, and there is a lot of things that can be the cause of the issue here. So, let's start with a better error trace and the file structure of your project. |
@mohmyo Thanks for replying also, I just solved this issue a few minutes back but I'm experiencing a very strange problem. I mean after customizing the PasswordResetSerializer, I made a folder templates/registration and put the templates inside, the strange part is, it's not getting recognized. I mean I have to define it like this -
Otherwise, it's not getting recognized if I'll just do |
There is no need to do So it should be like what I have posted earlier here from dj_rest_auth.serializers import PasswordResetSerializer
class MyPasswordResetSerializer(PasswordResetSerializer):
def get_email_options(self) :
return {
'email_template_name': 'password_reset_email.html'
} The issue for template folder not recognized may be related to some configuration to your project.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../', 'templates'), ], # note that ../ is here because our base.py is under two levels
'APP_DIRS': True,
'OPTIONS': {
........
},
},
] |
@mohmyo Just one more small help please, I mean we have a directory just like below so how should I add that? What should I put in 'DIRS' as I have templates folder according to different apps.
Thanks for all your help. |
I think this will help you a lot to pick the right |
@mohmyo Yeah actually I already tried that out but didn't get it working that's why I asked you in case I was making some mistake. The error looks something like this -
Thanks for your support. |
can you share your |
Here, this is the complete file, and it is in, BASE_DIR/apps/accounts/registration/password_reset_email.html.
Thanks |
I think you can try this TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../', 'apps/app1/templates'), ],
'APP_DIRS': True,
'OPTIONS': {
........
},
},
]
|
Hi @mohmyo , I've a question that in PasswordResetView , Initially Link going on email is "http://127.0.0.1:8000/........". I want to change it to the other URL(i.e. url on which Frontend(Angular) is hosted on). How can I do it? Thanks in advance. |
Just a heads-up: this works till version 2.1.4. Later versions will ignore |
Here is how I got it running. These are custom serializers:
You also have to set custom serializers in settings.py |
For anyone attempting to do this with the following dependencies:
None of the above answers fully worked for me as most options were deprecated after Customising reset password URL
|
I add uid base_message.txt and password_reset_key_message.txt files can added for custom template (base_message.txt duplicated) The answer above was the most suitable for me. (django 5 dj-rest-auth 5) |
Hey @Erwol, thank you so much! What you suggested really helped me out. I was stuck on this problem for a few days, but now I've got it sorted. |
My main goal is to customize the URL returned to the user in the mail so it uses the base URL of my frontend not the base URL of my backend
The text was updated successfully, but these errors were encountered: