How can I change the domain the link inside the email djoser sends uses?
I figured this out, to change the link domain you need to add DOMAIN and SITE_NAME to your project settings. Example:
DOMAIN = config('DOMAIN') #example.com
SITE_NAME = config('SITE_NAME') #Example
DJOSER = {
'LOGIN_FIELD':'email',
'USER_CREATE_PASSWORD_RETYPE':True,
'ACTIVATION_URL': '/users/activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'SERIALIZERS':{
'user_create':'userauth.serializers.UserCreateSerializer',
'user':'userauth.serializers.UserCreateSerializer',
'activation': 'djoser.email.ActivationEmail',
}
Then you should get the next link in your email:
http://example.com/auth/users/activate/MQ/5c9-26bcab9e85e8a967731d
{{ protocol }} variable in email templates rised from templated_mail library,
protocol = context.get('protocol') or (
'https' if self.request.is_secure() else 'http'
)
so, with https requests on production server protocol will be https
as mentioned, for the "domain" and "site_name", it can be defined in settings.py:
DOMAIN = "example.com"
SITE_NAME = "Example"
but for the protocol there is two approaches:
1- I didn't test it, but it should works,
in a production environment if you add in your setting: SECURE_SSL_REDIRECT=True, it will change the default protocol in emails from "http" to "https"
2- anyway, you can override the ActivationEmail (or other email functions) to force them to change the protocol from "http" to "https"
class ActivationEmail(BaseEmailMessage):
template_name = 'email/activation.html'
def get_context_data(self):
# ActivationEmail can be deleted
context = super().get_context_data()
user = context.get("user")
context["uid"] = utils.encode_uid(user.pk)
context["token"] = default_token_generator.make_token(user)
context["url"] = settings.ACTIVATION_URL.format(**context)
context["protocol"] = "https"
return context
© 2022 - 2024 — McMap. All rights reserved.