Skip to main content

A Django slugify application that guarantees uniqueness and handles unicode

Project description

django-uuslug
================

A Django slugify application that guarantees uniqueness and handles unicode.
UUSlug = (``U``nique + ``U``nicode Slug)

Patches welcome: https://github.com/un33k/django-uuslug

Usage
=====

A. Install django-uuslug:
* _ Make sure you have python 2.6+ and can install from pypi
1. easy_install django-uuslug
2. pip install django-uuslug
3. git clone http://github.com/un33k/django-uuslug
a. cd django-uuslug
b. run python setup.py
4. wget https://github.com/un33k/django-uuslug/zipball/master
a. unzip the downloaded file
b. cd into django-uuslug-* directory
c. run python setup.py

B. Test & contribute to django-uuslug: (for developers)
* _ git clone http://github.com/un33k/django-uuslug
a. cd into django-uuslug
b. run python bootstrap.py
c. run bin/buildout -vvvvv
d. run bin/test


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-lete")

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) # slug = "john"

c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1") # slug = "john-1"

c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-2") # slug = "john-2"

ToDo:
=====
clean up README
add more test and examples

Credits:
=========
This is inspired by a snippet from http://www.djangosnippets.org/
Improved to handle unique slugs and unicode chars and packaged for easy install.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-uuslug-0.7.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file django-uuslug-0.7.tar.gz.

File metadata

  • Download URL: django-uuslug-0.7.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-uuslug-0.7.tar.gz
Algorithm Hash digest
SHA256 bec7a80551cc5dcbb34c04ce46852f63648ad2b26631e3f076a034eaf398dc85
MD5 8aba8d94dcf9cb49468adde649e1a900
BLAKE2b-256 89541a6e9295e820b588b56fa668e88795184cf650dc2c909a6be73a5b2aa506

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page