Opened 10 years ago
Last modified 2 years ago
#22752 new Cleanup/optimization
Allow PasswordResetForm email to render URLs based on the current namespace
Reported by: | Ben Davis | Owned by: | nobody |
---|---|---|---|
Component: | contrib.auth | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Ben Davis | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have multiple namespace instances for password reset urls. The email template rendered by the default PasswordResetForm does not included a current_app
context. It's an easy fix, though, ass the PasswordResetForm already has self.current_app
. Just need to wrap the context instance passed to the email template:
-
django/contrib/auth/forms.py
diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index 6e07d45..baef873 100644
a b from collections import OrderedDict 4 4 5 5 from django import forms 6 6 from django.forms.utils import flatatt 7 from django.template import loader 7 from django.template import loader, Context 8 8 from django.utils.encoding import force_bytes 9 9 from django.utils.html import format_html, format_html_join 10 10 from django.utils.http import urlsafe_base64_encode … … class PasswordResetForm(forms.Form): 264 264 'token': token_generator.make_token(user), 265 265 'protocol': 'https' if use_https else 'http', 266 266 } 267 subject = loader.render_to_string(subject_template_name, c) 267 context_instance = Context(current_app=self.current_app) 268 subject = loader.render_to_string( 269 subject_template_name, c, context_instance) 268 270 # Email subject *must not* contain newlines 269 271 subject = ''.join(subject.splitlines()) 270 email = loader.render_to_string(email_template_name, c) 272 email = loader.render_to_string( 273 email_template_name, c, context_instance) 271 274 272 275 if html_email_template_name: 273 276 html_email = loader.render_to_string(html_email_template_name, c)
Change History (6)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Easy pickings: | unset |
---|
comment:3 by , 10 years ago
Has patch: | set |
---|---|
Needs documentation: | set |
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
Hi,
I agree that it would be good to have the current app while rendering the body of the email because it would help with reversing URLs inside it.
However, I don't really understand the use-case behind having the current app when rendering the subject line.
In any case, the provided patch doesn't apply on master and it's also going to require tests and documentation.
Thanks.
comment:4 by , 10 years ago
@bmispelon, I think one would expect the context to be identical in both the body and the subject. Whether or not there's a use case, I can't think of any justification for making them separate. I plan on working on this (as well as various other tickets I've submitted) once I get a little more free time.
comment:5 by , 10 years ago
Cc: | added |
---|
comment:7 by , 4 years ago
Has patch: | unset |
---|---|
Needs documentation: | unset |
Needs tests: | unset |
Patch needs improvement: | unset |
Summary: | PasswordResetForm email context is missing current_app → Allow PasswordResetForm email to render URLs based on the current namespace |
current_app
is no longer an argument to render_to_string()
but I think this would be resolved by passing request
instead (which can be passed to send_mail()
from save()
). The url
template tag uses the namespace of the currently resolved view as the current application in a RequestContext
.
Correction, PasswordResetForm does not have current_app built in. That needs to be added as well.