2

I am making search for website with multiple languages. It's working fine, but when it comes to Russian, i get problems. Django doesn't process Russian characters. In template i have

<input type="text" name="q">

When i type russian text, like ванна,in my view function in request.POST['q'] i have that word correctly. Then i need to slugify this, but it just gives me empty string. Also i tried this answer, but then i get result vanna, when i need it to be the same Russian string. Maybe there is some way converting it back? Or any other solution?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Mažas
  • 387
  • 1
  • 6
  • 19

1 Answers1

4

From documentation:

Converts to ASCII if allow_unicode is False (default). Converts spaces to hyphens. Removes characters that aren’t alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace.

This should work:

slugify("ванна", allow_unicode=True)

This is only available as of Django 1.9 though.

However, based on Django 1.9 source code, you can create your own utils function:

from __future__ import unicode_literals

import re
import unicodedata

from django.utils import six
from django.utils.encoding import force_text
from django.utils.functional import allow_lazy
from django.utils.safestring import SafeText, mark_safe

def slugify_unicode(value):
    value = force_text(value)
    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))
slugify_unicode = allow_lazy(slugify_unicode, six.text_type, SafeText)
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • Ooh I hadn't realised they'd updated this, [correct doc link](https://docs.djangoproject.com/en/1.9/ref/utils/#django.utils.text.slugify) btw – Sayse Mar 23 '16 at 10:30
  • Well, im using django 1.6.11, i can't do this. – Mažas Mar 23 '16 at 10:31
  • Then i get error: `sub() got an unexpected keyword argument 'flags'` for this line: `value = re.sub('[^\w\s-]', '', value, flags=re.U).strip().lower() ` – Mažas Mar 23 '16 at 12:01
  • what is your python version? – Antoine Pinsard Mar 23 '16 at 12:12
  • This is weird. The `flags` argument was added in python 2.7 (https://docs.python.org/2/library/re.html#re.sub). What is the signature of the method? (`import re; help(re.sub)`) – Antoine Pinsard Mar 24 '16 at 09:37