Fixed #28694 -- Made django.utils.text.slugify() strip dashes and underscores.

This commit is contained in:
David Smith 2020-05-26 22:56:41 +01:00 committed by Mariusz Felisiak
parent 3111b434e7
commit 0382ecfe02
4 changed files with 16 additions and 4 deletions

View File

@ -396,15 +396,15 @@ def slugify(value, allow_unicode=False):
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics, dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace. trailing whitespace, dashes, and underscores.
""" """
value = str(value) value = str(value)
if allow_unicode: if allow_unicode:
value = unicodedata.normalize('NFKC', value) value = unicodedata.normalize('NFKC', value)
else: else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value.lower()).strip() value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value) return re.sub(r'[-\s]+', '-', value).strip('-_')
def camel_case_to_spaces(value): def camel_case_to_spaces(value):

View File

@ -853,8 +853,8 @@ appropriate entities.
#. Converting to lowercase. #. Converting to lowercase.
#. Removing characters that aren't alphanumerics, underscores, hyphens, or #. Removing characters that aren't alphanumerics, underscores, hyphens, or
whitespace. whitespace.
#. Removing leading and trailing whitespace.
#. Replacing any whitespace or repeated dashes with single dashes. #. Replacing any whitespace or repeated dashes with single dashes.
#. Removing leading and trailing whitespace, dashes, and underscores.
For example:: For example::
@ -867,6 +867,11 @@ appropriate entities.
>>> slugify('你好 World', allow_unicode=True) >>> slugify('你好 World', allow_unicode=True)
'你好-world' '你好-world'
.. versionchanged:: 3.2
In older versions, leading and trailing dashes and underscores are not
removed.
.. _time-zone-selection-functions: .. _time-zone-selection-functions:
``django.utils.timezone`` ``django.utils.timezone``

View File

@ -280,6 +280,9 @@ Miscellaneous
* :attr:`.ModelAdmin.prepopulated_fields` no longer strips English stop words, * :attr:`.ModelAdmin.prepopulated_fields` no longer strips English stop words,
such as ``'a'`` or ``'an'``. such as ``'a'`` or ``'an'``.
* :func:`~django.utils.text.slugify` now removes leading and trailing dashes
and underscores.
.. _deprecated-features-3.2: .. _deprecated-features-3.2:
Features deprecated in 3.2 Features deprecated in 3.2

View File

@ -195,6 +195,10 @@ class TestUtilsText(SimpleTestCase):
(' multiple---dash and space ', 'multiple-dash-and-space', False), (' multiple---dash and space ', 'multiple-dash-and-space', False),
('\t whitespace-in-value \n', 'whitespace-in-value', False), ('\t whitespace-in-value \n', 'whitespace-in-value', False),
('underscore_in-value', 'underscore_in-value', False), ('underscore_in-value', 'underscore_in-value', False),
('__strip__underscore-value___', 'strip__underscore-value', False),
('--strip-dash-value---', 'strip-dash-value', False),
('__strip-mixed-value---', 'strip-mixed-value', False),
('_ -strip-mixed-value _-', 'strip-mixed-value', False),
('spam & ıçüş', 'spam-ıçüş', True), ('spam & ıçüş', 'spam-ıçüş', True),
('foo ıç bar', 'foo-ıç-bar', True), ('foo ıç bar', 'foo-ıç-bar', True),
(' foo ıç bar', 'foo-ıç-bar', True), (' foo ıç bar', 'foo-ıç-bar', True),