Skip to content

Commit

Permalink
Fixed #28694 -- Made django.utils.text.slugify() strip dashes and und…
Browse files Browse the repository at this point in the history
…erscores.
  • Loading branch information
smithdc1 authored and felixxm committed May 29, 2020
1 parent 3111b43 commit 0382ecf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions django/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion docs/ref/utils.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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``
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/3.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/utils_tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def test_slugify(self):
(' 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),
Expand Down

0 comments on commit 0382ecf

Please sign in to comment.