PS:好吧,我真的不想发表文章,为什么呢?因为我是一个菜鸡,特别菜的那种,笔记,脑图一大堆,但是仍然菜的一匹,唉,不说了......
首先吧,学习Django第一个遇上的坑——slug字段,为啥呢?这货不懂中文.....
一、介绍:
django-uuslug是一个很方便的将中文转化成拼音slug的插件. 通过使用django-uuslug, 可以保确保slug是唯一的, 并且都是unicode编码的。
二、安装:
pip install django-uuslug
三、使用:
重写model.py中的save()方法:
from django.db import models
from uuslug import uuslug
class CoolSlug(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
# self.slug = uuslug(self.name, instance=self, separator="_")
# optional non-dash separator
self.slug = uuslug(self.name, instance=self, start_no=2)
super(CoolSlug, self).save(*args, **kwargs)
默认:
-
去首尾符号
-
多个中划线变成一个
-
中文翻译成拼音用"-"隔开
separator 指定隔开字符:
separator="字符"
start_no 指定起始数字:
start_no=数字
max_length 限制slug的长短:
max_length=数字
word_boundary 字边界:配合mex_length在保存单词完整性下,保证slug的限制长短
word_boundary=True
save_order 保存顺序?:配合mex_length和word_boundary来保证字符长短,完整性下的顺序。(我TM在讲什么?)即在前两者满足情况下,如果save_order为false,在长度不够的情况下,可能会选择后面较短的字符。
save_order=True
过滤某些字符,接收参数为一个数组。该方法大小写不灵敏。
stopwords=['字符', '字符']
Django外使用
这个我直接copy的geihub
####### Unicode Test #######
from uuslug import slugify
txt = "This is a test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
txt = "___This is a test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
txt = "___This is a test___"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
txt = '影師嗎'
r = slugify(txt)
self.assertEqual(r, "ying-shi-ma")
txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEqual(r, "c-est-deja-l-ete")
txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(txt)
self.assertEqual(r, "nin-hao-wo-shi-zhong-guo-ren")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt)
self.assertEqual(r, "jaja-lol-mememeoo-a")
txt = 'Компьютер'
r = slugify(txt)
self.assertEqual(r, "kompiuter")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=9)
self.assertEqual(r, "jaja-lol")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=15)
self.assertEqual(r, "jaja-lol-mememe")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=50)
self.assertEqual(r, "jaja-lol-mememeoo-a")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=15, word_boundary=True)
self.assertEqual(r, "jaja-lol-a")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=17, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=18, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=19, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo-a")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=20, word_boundary=True, separator=".")
self.assertEqual(r, "jaja.lol.mememeoo.a")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=20, word_boundary=True, separator="ZZZZZZ")
self.assertEqual(r, "jajaZZZZZZlolZZZZZZmememeooZZZZZZa")
txt = 'one two three four five'
r = slugify(txt, max_length=13, word_boundary=True, save_order=True)
self.assertEqual(r, "one-two-three")
txt = 'one two three four five'
r = slugify(txt, max_length=13, word_boundary=True, save_order=False)
self.assertEqual(r, "one-two-three")
txt = 'one two three four five'
r = slugify(txt, max_length=12, word_boundary=True, save_order=False)
self.assertEqual(r, "one-two-four")
txt = 'one two three four five'
r = slugify(txt, max_length=12, word_boundary=True, save_order=True)
self.assertEqual(r, "one-two")
txt = 'this has a stopword'
r = slugify(txt, stopwords=['stopword'])
self.assertEqual(r, 'this-has-a')
txt = 'the quick brown fox jumps over the lazy dog'
r = slugify(txt, stopwords=['the'])
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
txt = 'Foo A FOO B foo C'
r = slugify(txt, stopwords=['foo'])
self.assertEqual(r, 'a-b-c')
txt = 'Foo A FOO B foo C'
r = slugify(txt, stopwords=['FOO'])
self.assertEqual(r, 'a-b-c')
txt = 'the quick brown fox jumps over the lazy dog in a hurry'
r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
####### Uniqueness Test #######
from django.db import models
from uuslug import uuslug
# Override your object's save method with something like this (models.py)
class CoolSlug(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = uuslug(self.name, instance=self)
super(CoolSlug, self).save(*args, **kwargs)
# Note: You can also specify the start number.
# Example:
self.slug = uuslug(self.name, instance=self, start_no=2)
# the second slug should start with "-2" instead of "-1"
name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
print(c.slug) # => "john"
c1 = CoolSlug.objects.create(name=name)
c1.save()
print(c1.slug) # => "john-1"
c2 = CoolSlug.objects.create(name=name)
c2.save()
print(c2.slug) # => "john-2"
# If you need truncation of your slug to exact length, here is an example
class SmartTruncatedSlug(models.Model):
name = models.CharField(max_length=19)
slug = models.CharField(max_length=10)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = uuslug(self.name, instance=self, max_length=10)
super(SmartTruncatedSlug, self).save(*args, **kwargs)
# If you need automatic truncation of your slug, here is an example
class AutoTruncatedSlug(models.Model):
name = models.CharField(max_length=19)
slug = models.CharField(max_length=19)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = uuslug(self.name, instance=self)
super(SmartTruncatedSlug, self).save(*args, **kwargs)
网友评论