Fixed #29746 -- Fixed misleading FlatpageForm URL help text if APPEND_SLASH is disabled.
This commit is contained in:
parent
5195b99e2c
commit
a43cfc23d4
|
@ -22,6 +22,19 @@ class FlatpageForm(forms.ModelForm):
|
||||||
model = FlatPage
|
model = FlatPage
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
if not self._trailing_slash_required():
|
||||||
|
self.fields['url'].help_text = _(
|
||||||
|
"Example: '/about/contact'. Make sure to have a leading slash."
|
||||||
|
)
|
||||||
|
|
||||||
|
def _trailing_slash_required(self):
|
||||||
|
return (
|
||||||
|
settings.APPEND_SLASH and
|
||||||
|
'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE
|
||||||
|
)
|
||||||
|
|
||||||
def clean_url(self):
|
def clean_url(self):
|
||||||
url = self.cleaned_data['url']
|
url = self.cleaned_data['url']
|
||||||
if not url.startswith('/'):
|
if not url.startswith('/'):
|
||||||
|
@ -29,9 +42,7 @@ class FlatpageForm(forms.ModelForm):
|
||||||
gettext("URL is missing a leading slash."),
|
gettext("URL is missing a leading slash."),
|
||||||
code='missing_leading_slash',
|
code='missing_leading_slash',
|
||||||
)
|
)
|
||||||
if (settings.APPEND_SLASH and
|
if self._trailing_slash_required() and not url.endswith('/'):
|
||||||
'django.middleware.common.CommonMiddleware' in settings.MIDDLEWARE and
|
|
||||||
not url.endswith('/')):
|
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
gettext("URL is missing a trailing slash."),
|
gettext("URL is missing a trailing slash."),
|
||||||
code='missing_trailing_slash',
|
code='missing_trailing_slash',
|
||||||
|
|
|
@ -49,6 +49,11 @@ class FlatpageAdminFormTests(TestCase):
|
||||||
def test_flatpage_requires_trailing_slash_with_append_slash(self):
|
def test_flatpage_requires_trailing_slash_with_append_slash(self):
|
||||||
form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
|
form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
|
||||||
with translation.override('en'):
|
with translation.override('en'):
|
||||||
|
self.assertEqual(
|
||||||
|
form.fields['url'].help_text,
|
||||||
|
"Example: '/about/contact/'. Make sure to have leading and "
|
||||||
|
"trailing slashes."
|
||||||
|
)
|
||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(form.errors['url'], ["URL is missing a trailing slash."])
|
self.assertEqual(form.errors['url'], ["URL is missing a trailing slash."])
|
||||||
|
|
||||||
|
@ -56,6 +61,11 @@ class FlatpageAdminFormTests(TestCase):
|
||||||
def test_flatpage_doesnt_requires_trailing_slash_without_append_slash(self):
|
def test_flatpage_doesnt_requires_trailing_slash_without_append_slash(self):
|
||||||
form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
|
form = FlatpageForm(data=dict(url='/no_trailing_slash', **self.form_data))
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
|
with translation.override('en'):
|
||||||
|
self.assertEqual(
|
||||||
|
form.fields['url'].help_text,
|
||||||
|
"Example: '/about/contact'. Make sure to have a leading slash."
|
||||||
|
)
|
||||||
|
|
||||||
def test_flatpage_admin_form_url_uniqueness_validation(self):
|
def test_flatpage_admin_form_url_uniqueness_validation(self):
|
||||||
"The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
|
"The flatpage admin form correctly enforces url uniqueness among flatpages of the same site"
|
||||||
|
|
Loading…
Reference in New Issue