2016-03-23 53 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()' –