标签:
django-uuslug是一个很方便的将中文转化成拼音slug的插件. 通过使用django-uuslug, 可以保确保slug是唯一的, 并且都是unicode编码的.
django中的slug是什么: slug是一个新闻属性, django中的slug是指有效URL的一部分, 能使URL更加清晰易懂. 比如有这样一篇文章, 标题是"那个13岁的孩子", 它的URL地址是"/news/na-ge-13-sui-de-hai-zi", 后面这一部分便是slug.
使用VirtualEnv在Bash shell中:
pip install django-uuslug
from uuslug import slugify
txt = "This is a test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")
txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")
txt = ‘C\‘est déjà l\‘été.‘
r = slugify(txt)
self.assertEquals(r, "cest-deja-lete")
txt = ‘你好我是中国人‘
r = slugify(txt)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
txt = ‘Компьютер‘
r = slugify(txt)
self.assertEquals(r, "kompiuter")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt)
self.assertEquals(r, "jaja-lol-mememeoo-a")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt, max_length=9)
self.assertEquals(r, "jaja-lol")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt, max_length=15)
self.assertEquals(r, "jaja-lol-mememe")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt, max_length=50)
self.assertEquals(r, "jaja-lol-mememeoo-a")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt, max_length=15, word_boundary=True)
self.assertEquals(r, "jaja-lol-a")
txt = ‘jaja---lol-méméméoo--a‘
r = slugify(txt, max_length=19, word_boundary=True)
self.assertEquals(r,