2016-03-23 58 views
1

我正在搜索多种语言的网站。它工作得很好,但是谈到俄罗斯时,我遇到了问题。 Django不处理俄文字符。在模板中,我有django slugify俄语字符串

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

当我输入俄文本,像ванна,在request.POST['q']我认为我的功能有正确的那个词。然后我需要塞住这个,但它只是给了我空的字符串。我也试过这answer,但后来我得到结果vanna,当我需要它是相同的俄文字符串。也许有一些方法将其转换回来?或者其他解决方案?

回答

3

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.

这应该工作:

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

这仅作为Django的1.9,虽然的。

然而,根据Django 1.9 source code,您可以创建自己的utils的功能:

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) 
+0

噢,我还没有意识到他们会更新这个,[正确的文档链接](HTTPS://docs.djangoproject。 com/en/1.9/ref/utils /#django.utils.text.slugify)btw – Sayse

+0

那么,即时通讯使用django 1.6.11,我不能这样做。 –

+0

然后我得到错误:'sub()得到了一个意想不到的关键字参数'flags''这一行:'value = re.sub('[^ \ w \ s-]','',value,flags = re。 U).strip()。lower()' –