mozilla / unicode-slugify

A slugifier that works in unicode
BSD 3-Clause "New" or "Revised" License
321 stars 52 forks source link

Working example code #11

Open sunjoomoon opened 10 years ago

sunjoomoon commented 10 years ago
#models.py
from slugify import slugify, smart_text

title = models.CharField(_('file name'), max_length=255)
slug = slugify(_('url'), max_length=250, unique=True)
summary = models.TextField(_('summary'))

As a newbie, having tried many but without success, any working example ?

davedash commented 10 years ago

A very simple example:

>>> import slugify
>>> slugify.slugify('hello there')
u'hello-there'
ad-m commented 8 years ago

@sunjoomoon, to use with django you need django-autoslug then...

#models.py
from django.db import models
from autoslug import AutoSlugField

class Article(models.Model):
    '''An article with title, date and slug. The slug is not totally
    unique but there will be no two articles with the same slug within
    any month.
    '''
    title = models.CharField(max_length=200)
    pub_date = models.DateField(auto_now_add=True)
    slug = AutoSlugField(populate_from='title', unique_with='pub_date__month')
#settings.py
AUTOSLUG_SLUGIFY_FUNCTION = 'slugify.slugify'

or

#models.py
from django.db import models
from autoslug import AutoSlugField
from slugify import slugify

class Article(models.Model):
    '''An article with title, date and slug. The slug is not totally
    unique but there will be no two articles with the same slug within
    any month.
    '''
    title = models.CharField(max_length=200)
    pub_date = models.DateField(auto_now_add=True)
    slug = AutoSlugField(populate_from='title', unique_with='pub_date__month', slugify=slugify)

No settings.py changes.