mirror of
https://github.com/Hopiu/django-uuslug.git
synced 2026-05-22 00:21:52 +00:00
83 lines
2.1 KiB
Text
83 lines
2.1 KiB
Text
django-uuslug
|
|
================
|
|
|
|
A custom slug app for Django, that guarantees Double (U)s for (Uniqueness & Unicode handling)
|
|
|
|
Most of the code is originally by snippet http://www.djangosnippets.org/snippets/369/
|
|
Improved slightly to handle unique slugs and unicode chars and packaged by Val L33.
|
|
|
|
Patches welcome: https://github.com/un33k/django-uuslug
|
|
|
|
Usage
|
|
=====
|
|
|
|
If you want a worry free slug-er, then this is for you.
|
|
It handles unicode and it produces unique slugs.
|
|
|
|
Here what you need to do:
|
|
1. Install (via source code on github) or (pip install django-uuslug) ... etc.
|
|
2. Then import it like: from uuslug import uuslug as slugify
|
|
|
|
That's it.
|
|
|
|
Unicode Test Example
|
|
=====
|
|
|
|
from uuslug import uuslug as slugify
|
|
|
|
s = "This is a test ---"
|
|
r = slugify(s)
|
|
self.assertEquals(r, "this-is-a-test")
|
|
|
|
s = 'C\'est déjà l\'été.'
|
|
r = slugify(s)
|
|
self.assertEquals(r, "c-est-deja-l-ete")
|
|
|
|
s = 'Nín hǎo. Wǒ shì zhōng guó rén'
|
|
r = slugify(s)
|
|
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
|
|
|
|
s = '影師嗎'
|
|
r = slugify(s)
|
|
self.assertEquals(r, "ying-shi-ma")
|
|
|
|
|
|
Uniqueness Test Example
|
|
=====
|
|
Override your object's save method with something like this (models.py)
|
|
|
|
from django.db import models
|
|
from uuslug import uuslug as slugify
|
|
|
|
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 = slugify(self.name, instance=self)
|
|
super(CoolSlug, self).save(*args, **kwargs)
|
|
|
|
Test:
|
|
|
|
name = "john"
|
|
c = CoolSlug.objects.create(name=name)
|
|
c.save()
|
|
self.assertEquals(c.slug, name)
|
|
|
|
c1 = CoolSlug.objects.create(name=name)
|
|
c1.save()
|
|
self.assertEquals(c1.slug, name+"-1")
|
|
|
|
Notice:
|
|
the slug for the first object would be "john", while the second object's slug
|
|
would be "john-1".
|
|
|
|
ToDo
|
|
=====
|
|
clean up README
|
|
|
|
|
|
|