如何使 Django slugify 与 Unicode 字符串正常工作?
如何防止 slugify
过滤器剥离非 ASCII 字母数字字符? (我使用的是 Django 1.0.2)
cnprog.com 在问题 URL 中有中文字符,所以我查看了他们的代码。 他们没有在模板中使用 slugify
,而是在 Question
模型中调用此方法来获取永久链接。
def get_absolute_url(self):
return '%s%s' % (reverse('question', args=[self.id]), self.title)
他们是否对 URL 进行了 slugify 处理?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有一个名为 unidecode 的 python 包,我已将其用于 Askbot 问答论坛,它对于基于拉丁语的字母表效果很好,甚至对于希腊语看起来也很合理:
它对亚洲语言做了一些奇怪的事情:
这有意义吗?
在askbot中,我们像这样计算slugs:
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:
It does something weird with asian languages:
Does this make sense?
In askbot we compute slugs like so:
对于 Django >= 1.9,
django.utils.text.slugify
有一个allow_unicode
参数:如果您使用 Django <= 1.8(自 2018 年 4 月起就不应该使用),你可以从 Django 1.9 获取代码。
With Django >= 1.9,
django.utils.text.slugify
has aallow_unicode
parameter:If you use Django <= 1.8 (which you should not since April 2018), you can pick up the code from Django 1.9.
Mozilla 网站团队一直致力于实施:
https://github.com/mozilla/unicode-slugify
示例代码位于
http://davedash.com/2011/03/24/how-we-slug -at-mozilla/
The Mozilla website team has been working on an implementation :
https://github.com/mozilla/unicode-slugify
sample code at
http://davedash.com/2011/03/24/how-we-slug-at-mozilla/
此外,Django 版本的 slugify 不使用 re.UNICODE 标志,因此它甚至不会尝试理解
\w\s
的含义,因为它涉及非 ascii 字符。这个自定义版本对我来说效果很好:
请注意最后的正则表达式替换。 这是针对更稳健的表达式
r'\W'
问题的解决方法,它似乎会删除一些非 ascii 字符或错误地重新编码它们,如以下 python 解释器所示会话:我不确定上面的问题是什么,但我猜测它源于“无论是什么在 Unicode 字符属性数据库中被分类为字母数字,”以及它是如何实现的。 我听说 python 3.x 对更好的 unicode 处理有很高的优先级,所以这个问题可能已经被修复了。 或者,也许这是正确的 python 行为,而我滥用了 unicode 和/或中文。
目前,解决方法是避免使用字符类,并根据显式定义的字符集进行替换。
Also, the Django version of slugify doesn't use the re.UNICODE flag, so it wouldn't even attempt to understand the meaning of
\w\s
as it pertains to non-ascii characters.This custom version is working well for me:
Note the last regex substitution. This is a workaround to a problem with the more robust expression
r'\W'
, which seems to either strip out some non-ascii characters or incorrectly re-encode them, as illustrated in the following python interpreter session:I am unsure what the problem is above, but I'm guessing that it stems from "whatever is classified as alphanumeric in the Unicode character properties database," and how that is implemented. I have heard that python 3.x has a high priority on better unicode handling, so this may be fixed already. Or, maybe it is correct python behavior, and I am misusing unicode and/or the Chinese language.
For now, a work-around is to avoid character classes, and make substitutions based on explicitly defined character sets.
恐怕 django 的 slug 定义意味着 ascii,尽管 django 文档没有明确说明这一点。 这是 slugify 的默认过滤器的来源...您可以看到这些值正在转换为 ascii,并在出现错误时使用“忽略”选项:
基于此,我猜测 cnprog.com 不是使用官方
slugify
函数。 如果您想要不同的行为,您可能希望调整上面的 django 代码片段。尽管如此,URL 的 RFC 确实规定非 us-ascii 字符(或者更具体地说,除了字母数字和 $-_.+!*'() 以外的任何字符)应该使用 %hex 表示法进行编码。 如果您查看浏览器发送的实际原始 GET 请求(例如,使用 Firebug),您会发现中文字符实际上在发送之前已进行编码...浏览器只是使其在显示中看起来很漂亮。 我怀疑这就是为什么 slugify 坚持只使用 ascii,fwiw。
I'm afraid django's definition of slug means ascii, though the django docs don't explicitly state this. This is the source of the defaultfilters for the slugify... you can see that the values are being converted to ascii, with the 'ignore' option in case of errors:
Based on that, I'd guess that cnprog.com is not using an official
slugify
function. You may wish to adapt the django snippet above if you want a different behaviour.Having said that, though, the RFC for URLs does state that non-us-ascii characters (or, more specifically, anything other than the alphanumerics and $-_.+!*'()) should be encoded using the %hex notation. If you look at the actual raw GET request that your browser sends (say, using Firebug), you'll see that the chinese characters are in fact encoded before being sent... the browser just makes it look pretty in the display. I suspect this is why slugify insists on ascii only, fwiw.
您可能想看看:
https://github.com/un33k/django-uuslug
它会为你处理两个“U”。 U 表示唯一,U 表示 Unicode。
它将为您轻松完成这项工作。
You might want to look at:
https://github.com/un33k/django-uuslug
It will take care of both "U"s for you. U in unique and U in Unicode.
It will do the job for you hassle free.
这就是我使用的:
http://trac.django -fr.org/browser/site/trunk/djangofr/links/slughifi.py
SlugHiFi 是常规 slugify 的包装器,不同之处在于它用对应的英文字母替换了国家字符。
因此,您得到的不是“Ą”,而是“A”,而不是“Ł” => “L”等。
This is what I use:
http://trac.django-fr.org/browser/site/trunk/djangofr/links/slughifi.py
SlugHiFi is a wrapper for regular slugify, with a difference that it replaces national chars with their English alphabet counterparts.
So instead of "Ą" you get "A", instead of "Ł" => "L", and so on.
我有兴趣在 slug 中只允许 ASCII 字符,这就是为什么我尝试对同一字符串的一些可用工具进行基准测试:
Unicode Slugify:
Django Uuslug:
很棒的 Slugify:
Python Slugify:
django.utils .text.slugify
与 Unicode:I am interested in allowing only ASCII characters in the slug this is why I tried to benchmark some of the available tools for the same string:
Unicode Slugify:
Django Uuslug:
Awesome Slugify:
Python Slugify:
django.utils.text.slugify
with Unidecode: