diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
index 9d6a9918bb..78101b447c 100644
a
|
b
|
class UserChangeForm(forms.ModelForm):
|
163 | 163 | super().__init__(*args, **kwargs) |
164 | 164 | password = self.fields.get("password") |
165 | 165 | if password: |
166 | | password.help_text = password.help_text.format("../password/") |
| 166 | password.help_text = password.help_text.format( |
| 167 | f"../../{self.instance.pk}/password/" |
| 168 | ) |
167 | 169 | user_permissions = self.fields.get("user_permissions") |
168 | 170 | if user_permissions: |
169 | 171 | user_permissions.queryset = user_permissions.queryset.select_related( |
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
index 98bf264040..b0f015dee7 100644
a
|
b
|
|
1 | 1 | import datetime |
2 | 2 | import re |
| 3 | import urllib.parse |
3 | 4 | from unittest import mock |
4 | 5 | |
5 | 6 | from django.contrib.auth.forms import ( |
… |
… |
from django.core.mail import EmailMultiAlternatives
|
22 | 23 | from django.forms import forms |
23 | 24 | from django.forms.fields import CharField, Field, IntegerField |
24 | 25 | from django.test import SimpleTestCase, TestCase, override_settings |
| 26 | from django.urls import reverse |
25 | 27 | from django.utils import translation |
26 | 28 | from django.utils.text import capfirst |
27 | 29 | from django.utils.translation import gettext as _ |
… |
… |
class UserChangeFormTest(TestDataMixin, TestCase):
|
867 | 869 | # value to render correctly |
868 | 870 | self.assertEqual(form.initial["password"], form["password"].value()) |
869 | 871 | |
| 872 | @override_settings(ROOT_URLCONF="auth_tests.urls_admin") |
| 873 | def test_bug_34066_link_to_password_reset_in_helptext(self): |
| 874 | user = User.objects.get(username="testclient") |
| 875 | form = UserChangeForm(data={}, instance=user) |
| 876 | passsword_help_text = form.fields["password"].help_text |
| 877 | matches = re.search('<a href="(.*?)">', passsword_help_text) |
| 878 | |
| 879 | # Get URL to UserChangeForm in admin via to_field (instead of pk) |
| 880 | admin_user_change_url = f"""{reverse( |
| 881 | "admin:%s_%s_change" % (user._meta.app_label, user._meta.model_name), |
| 882 | args=(user.username,), |
| 883 | )}?to_field=username""" |
| 884 | |
| 885 | # join relative url found in password's help_text to admin_user_change_url |
| 886 | joined_url = urllib.parse.urljoin(admin_user_change_url, matches.group(1)) |
| 887 | |
| 888 | # Get URL to PasswordChangeForm in admin |
| 889 | pw_change_url = reverse( |
| 890 | "admin:%s_%s_password_change" |
| 891 | % (user._meta.app_label, user._meta.model_name), |
| 892 | args=(user.pk,), |
| 893 | ) |
| 894 | # assert joined_url and pw_change_url are ideential |
| 895 | self.assertEqual(joined_url, pw_change_url) |
| 896 | |
870 | 897 | def test_custom_form(self): |
871 | 898 | class CustomUserChangeForm(UserChangeForm): |
872 | 899 | class Meta(UserChangeForm.Meta): |