Django-autoslug is a reusable Django library that provides an improved slug field which can automatically:
- populate itself from another field,
- preserve uniqueness of the value and
- use custom
slugify()
functions for better i18n.
The field is highly configurable.
Python 3.9+ or PyPy.
Django 3.2 or higher.
It may be possible to successfully use django-autoslug in other environments but they are not tested.
Note
PyPy3 is not officially supported only because there were some problems with
permissions and __pycache__
on CI unrelated to django-autoslug itself.
python -m pip install django-autoslug
A simple example:
from django.db.models import CharField, Model
from autoslug import AutoSlugField
class Article(Model):
title = CharField(max_length=200)
slug = AutoSlugField(populate_from='title')
More complex example:
from django.db.models import CharField, DateField, ForeignKey, Model
from django.contrib.auth.models import User
from autoslug import AutoSlugField
class Article(Model):
title = CharField(max_length=200)
pub_date = DateField(auto_now_add=True)
author = ForeignKey(User)
slug = AutoSlugField(populate_from=lambda instance: instance.title,
unique_with=['author__name', 'pub_date__month'],
slugify=lambda value: value.replace(' ','-'))
See the complete documentation on ReadTheDocs. It is built automatically for the latest version.
This application is maintained by Justin Mayer. It was initially created by
Andy Mikhailenko and then improved by other developers. They are listed in
AUTHORS.rst
.
Please feel free to file issues and/or submit patches.
See CONTRIBUTING.rst
for hints related to the preferred workflow.
Django-autoslug is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
Django-autoslug is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program; see the file COPYING.LESSER. If not, see GNU licenses.