diff options
| author | Edward Henderson <kutenai@me.com> | 2015-04-15 16:28:49 -0600 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-07-17 13:48:58 -0400 |
| commit | f8cc464452f495fce2a3d6f7494396c8f798a1e6 (patch) | |
| tree | 61049a8351e327b59c98341f98dacda0e8186be2 /django/utils | |
| parent | adffff79a36f7de30f438915c492e475e17025f6 (diff) | |
Fixed #16501 -- Added an allow_unicode parameter to SlugField.
Thanks Flavio Curella and Berker Peksag for the initial patch.
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/text.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/django/utils/text.py b/django/utils/text.py index 5d1f4a0998..456c853712 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -410,13 +410,17 @@ def unescape_string_literal(s): unescape_string_literal = allow_lazy(unescape_string_literal) -def slugify(value): +def slugify(value, allow_unicode=False): """ - Converts to ASCII. Converts spaces to hyphens. Removes characters that - aren't alphanumerics, underscores, or hyphens. Converts to lowercase. - Also strips leading and trailing whitespace. + Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens. + Remove characters that aren't alphanumerics, underscores, or hyphens. + Convert to lowercase. Also strip leading and trailing whitespace. """ value = force_text(value) + if allow_unicode: + value = unicodedata.normalize('NFKC', value) + value = re.sub('[^\w\s-]', '', value, flags=re.U).strip().lower() + return mark_safe(re.sub('[-\s]+', '-', value, flags=re.U)) value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub('[^\w\s-]', '', value).strip().lower() return mark_safe(re.sub('[-\s]+', '-', value)) |
