开发者

Django: Clean unicode for UrlEncoding

I am trying to take unicode and clean it to be used for URLs.

Examples : "Bird's Milk" Cake OR Pão com Ovo

In converting these, my goal is to make them as human readable as possible so, the urls following those examples would be - /birds-milk-cake/ or /pao-com-ovo/

To get the ASCII of the accented characters,

title = 'Pão com Ovo'
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore')

However I am wondering what the best solu开发者_高级运维tion is for removing characters like # ! ' " ( ) &. Normalize() errors on those characters so is there a proper way for removing those characters while retaining the accented characters?


There is an old, unmaintained but working code that extends django.template.defaultfilters.slugify() by adding support for all characters you can imagine. If you need to support all kinds of languages then this may be a good solution. It's called slughifi.


>>> from django.template.defaultfilters import slugify
>>> slugify("Pão com Ovo")
u'pao-com-ovo'
>>> slugify(""""Bird's Milk" Cake""")
u'birds-milk-cake'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜