如何使Django slugify使用Unicode字符串正常工作? [英] How to make Django slugify work properly with Unicode strings?

查看:95
本文介绍了如何使Django slugify使用Unicode字符串正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以采取什么措施来防止 slugify 过滤器从剥离非ASCII字母数字字符? (我使用Django 1.0.2)

What can I do to prevent slugify filter from stripping out non-ASCII alphanumeric characters? (I'm using Django 1.0.2)

cnprog.com 有问题的URL中的汉字,所以我看了他们的代码。他们没有在模板中使用 slugify ,而是在问题模型中调用此方法来获取固定链接


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)

他们是否阻止网址?

推荐答案

有一个名为 unidecode 我已经采用了askbot问答论坛,它适用于拉丁语的字母表,甚至对希腊来说看起来很合理:

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'

它与亚洲语言:

>>> unidecode(u'影師嗎')
'Ying Shi Ma '
>>> 

这是否有意义?

askbot我们计算slugs如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆