如何使 Django slugify 与 Unicode 字符串一起正常工作? [英] How to make Django slugify work properly with Unicode strings?
问题描述
如何防止 slugify
过滤器去除非 ASCII 字母数字字符?(我使用的是 Django 1.0.2)
cnprog.com 在问题网址中有汉字,所以我查看了他们的代码.他们没有在模板中使用 slugify
,而是在 Question
模型中调用这个方法来获取永久链接
def get_absolute_url(self):return '%s%s' % (reverse('question', args=[self.id]), self.title)
他们是否对 URL 进行了 slugifying?
有一个名为 unidecode 我在 askbot Q&A 论坛上采用的,它适用于基于拉丁语的字母表,甚至对于希腊语看起来也很合理:
<预><代码>>>>导入单解码>>>从 unidecode 导入 unidecode>>>unidecode(u'διακριτικός')'diakritikos'它对亚洲语言做了一些奇怪的事情:
<预><代码>>>>unidecode(u'影师吗')'英士马'>>>这有意义吗?
在 askbot 中,我们像这样计算 slug:
from unidecode import unidecode从 django.template 导入默认过滤器slug = defaultfilters.slugify(unidecode(input_text))
What can I do to prevent slugify
filter from stripping out non-ASCII alphanumeric characters? (I'm using Django 1.0.2)
cnprog.com has Chinese characters in question URLs, so I looked in their code. They are not using slugify
in templates, instead they're calling this method in Question
model to get permalinks
def get_absolute_url(self):
return '%s%s' % (reverse('question', args=[self.id]), self.title)
Are they slugifying the URLs or not?
There is a python package called unidecode that I've adopted for the askbot Q&A forum, it works well for the latin-based alphabets and even looks reasonable for greek:
>>> import unidecode
>>> from unidecode import unidecode
>>> unidecode(u'διακριτικός')
'diakritikos'
It does something weird with asian languages:
>>> unidecode(u'影師嗎')
'Ying Shi Ma '
>>>
Does this make sense?
In askbot we compute slugs like so:
from unidecode import unidecode
from django.template import defaultfilters
slug = defaultfilters.slugify(unidecode(input_text))
这篇关于如何使 Django slugify 与 Unicode 字符串一起正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!