'Stop djoser from adding API url to the frontend url set in PASSWORD_RESET_CONFIRM_URL
I have a DRF API using djoser to handle password reset. The password reset workflow is working well except for the PASSWORD_RESET_CONFIRM_URL
When I set my frontend page URL in that setting for Djoser it keeps concatenating the API URL to the frontend page URL. How can I stop that
for more context here are my Djoser settings showing the URL I set for PASSWORD_RESET_CONFIRM_URL
DJOSER = {
'PASSWORD_RESET_CONFIRM_URL': 'http://localhost:3000/auth/confirm-password-reset/{uid}/{token}',
'ACTIVATION_URL': '#/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND': True,
'PASSWORD_RESET_CONFIRM_RETYPE': True,
'SERIALIZERS': {
'password_reset': 'djoser.serializers.SendEmailResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'current_user': 'djoser.serializers.UserSerializer',
},
'PERMISSIONS': {
}
}
and here is the URL I receive in my email
as you can see http://127.0.0.1:8000 the API URL has been added to the frontend URL I set
Solution 1:[1]
There appear to be two ways to set a new DOMAIN name explicitly:
- Add a
DOMAINandSITE_NAMEto the Django settings - Use the Django Sites framework and set the domain name there
Sites isn't often useful, so use #1 unless something else fails. The only other option is overriding all email templates so you can specify the full url.
- Djoser only supports paths/fragments, not full urls
- Links are created using
{{ protocol }}://{{ domain }}/{{ url|safe }} - A package called
templated_mailis used to send these emails - That package sets the
protocol,domainandsite_name
The code for setting the variables for use in the email template is:
site = get_current_site(self.request)
domain = context.get('domain') or (
getattr(settings, 'DOMAIN', '') or site.domain
)
protocol = context.get('protocol') or (
'https' if self.request.is_secure() else 'http'
)
site_name = context.get('site_name') or (
getattr(settings, 'SITE_NAME', '') or site.name
)
The http vs https decision is made for you, but since those are almost always the same its probably not an issue in practice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew |

