Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working example code #11

Open
sunjoomoon opened this issue Sep 27, 2014 · 2 comments
Open

Working example code #11

sunjoomoon opened this issue Sep 27, 2014 · 2 comments

Comments

@sunjoomoon
Copy link

#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
Copy link
Contributor

A very simple example:

>>> import slugify
>>> slugify.slugify('hello there')
u'hello-there'

@ad-m
Copy link

ad-m commented Apr 16, 2016

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants