1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

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
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace.
trailing whitespace, dashes, and underscores.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value.lower()).strip()
return re.sub(r'[-\s]+', '-', value)
value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_')
def camel_case_to_spaces(value):

View File

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

View File

@ -280,6 +280,9 @@ Miscellaneous
* :attr:`.ModelAdmin.prepopulated_fields` no longer strips English stop words,
such as ``'a'`` or ``'an'``.
* :func:`~django.utils.text.slugify` now removes leading and trailing dashes
and underscores.
.. _deprecated-features-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),
('\t whitespace-in-value \n', 'whitespace-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),
('foo ıç bar', 'foo-ıç-bar', True),
(' foo ıç bar', 'foo-ıç-bar', True),