Resetting the password in Django has four main step;
I use two subdomains in my django project. The first two steps (1 & 2) must occur in one subdomain and the next two steps (3 & 4) must occur in the next subdomain
I Retrieve the password reset class to make some changes:
class CustomPasswordResetView(PasswordResetView):
template_name = "registration/password/password_set_form.html"
email_template_name = "registration/password/password_set_email.html"
subject_template_name = "registration/password/password_set_subject.html"
success_url = reverse_lazy('auth_staff:password_reset_done')
def dispatch(self, request, *args, **kwargs):
# Retrieve the username from the URL kwargs
self.username = kwargs.get('username')
if not self.username:
raise Http404("Username not provided in the URL.")
return super().dispatch(request, *args, **kwargs)
and this is default password_set_email.html:
{% load i18n %}{% autoescape off %}
{% blocktranslate %}You're receiving this email because you requested a password set for your user account at {{ site_name }}.{% endblocktranslate %}
{% translate "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'auth_staff:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve fotten:' %} {{ user.get_username }}
{% translate "Thanks for using our site!" %}
{% blocktranslate %}The {{ site_name }} team{% endblocktranslate %}
{% endautoescape %}
I want to change the {{ domain }} in password reset link. How can I do this?
Resetting the password in Django has four main step;
I use two subdomains in my django project. The first two steps (1 & 2) must occur in one subdomain and the next two steps (3 & 4) must occur in the next subdomain
I Retrieve the password reset class to make some changes:
class CustomPasswordResetView(PasswordResetView):
template_name = "registration/password/password_set_form.html"
email_template_name = "registration/password/password_set_email.html"
subject_template_name = "registration/password/password_set_subject.html"
success_url = reverse_lazy('auth_staff:password_reset_done')
def dispatch(self, request, *args, **kwargs):
# Retrieve the username from the URL kwargs
self.username = kwargs.get('username')
if not self.username:
raise Http404("Username not provided in the URL.")
return super().dispatch(request, *args, **kwargs)
and this is default password_set_email.html:
{% load i18n %}{% autoescape off %}
{% blocktranslate %}You're receiving this email because you requested a password set for your user account at {{ site_name }}.{% endblocktranslate %}
{% translate "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'auth_staff:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve fotten:' %} {{ user.get_username }}
{% translate "Thanks for using our site!" %}
{% blocktranslate %}The {{ site_name }} team{% endblocktranslate %}
{% endautoescape %}
I want to change the {{ domain }} in password reset link. How can I do this?
Since you are generating the email on subdomain A, but {{domain}}
needs to contain the address of subdomain B you need to override the {{domain}}
part of the email.
You can override the template with a custom template and semi-hardcode it, but I would not recommend that for if you later want to take a different approach.
The other option I found looking at the source code is overriding the save method of the PasswordResetForm
. Looking at the source code, it seems it receives a domain_override
argument which you could point to subdomain B. It would probably look something like this
class CustomPasswordResetForm(PasswordResetForm):
def save(*args, **kwargs):
domain_override = kwargs.pop('domain_override', 'subdomain B')
super().save(domain_override=domain_override, *args, **kwargs)
Then on your view just set this as the form_class
class CustomPasswordResetView(PasswordResetView):
...
form_class = CustomPasswordResetForm
This way you utilise what Django provides for this scenario without overcomplicating things or hardcoding them.
django.contrib.sites
in which case it looks for theSite
for the request domain. – willeM_ Van Onsem Commented Nov 17, 2024 at 0:04view
(or build it in middleware forrequest.domain
or using a context_processor similar to:{{ MY_DOMAIN }}
you need based on some type ofsettings
attr in the view or in middleware. You know where your subdoamins are, you can use Sites if this is getting complex but that depends on your use...I feel based on your question you can logically build the right url you need for the case pretty easily – ViaTech Commented Nov 17, 2024 at 1:21