最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to change the URL password reset domain in django? - Stack Overflow

programmeradmin3浏览0评论

Resetting the password in Django has four main step;

  1. Receive email from the user
  2. Send password reset link
  3. Get the link and change the password from the user side
  4. Register new password successfully

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;

  1. Receive email from the user
  2. Send password reset link
  3. Get the link and change the password from the user side
  4. Register new password successfully

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?

Share Improve this question asked Nov 16, 2024 at 23:58 cosmoscodercosmoscoder 454 bronze badges 4
  • It typically gets the one from the request unless, you use django.contrib.sites in which case it looks for the Site for the request domain. – willeM_ Van Onsem Commented Nov 17, 2024 at 0:04
  • 1 isn't step 1 receiving a Request, not an email. Do you use the first domain for something besides resetting? – willeM_ Van Onsem Commented Nov 17, 2024 at 0:25
  • actually, first subdomain is for staff and second for customers and special things happen in each of them – cosmoscoder Commented Nov 17, 2024 at 0:34
  • I really might be missing something, but why not just build the "domain" variable you need in the view (or build it in middleware for request.domainor using a context_processor similar to: {{ MY_DOMAIN }} you need based on some type of settings 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
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论