Skip to main content

pipeline for slugification edgecases in django

Project description

pipeline for handling slugification edgecases

Python Package Build Status Docs Code Coverage License

What are slugs?

Slugs are URL’s, typically generated from post titles, that you want to be both human readable and a valid URL. They are SEO friendly.

Django provides a slugify function in django.utils.text.slugify which is also made available as a default filter.

Django slugs can be automatically generated in django models via packages such as:

The problem

This project is based on an article from devel.tech covering django’s import strings.

Corner cases exist with slugification. For instance:

Term

django.utils.text.slugify

What you want

C

c (correct)

n/a

C++

c

cpp

C#

c

c-sharp

To make matters worse, if using a specialized model field like AutoSlugField from django-autoslug or django-extensions, the default behavior may be to name the slugs for C++ and C# to “c-1”, “c-2” after “c” is taken.

Here’s another case, acronyms / shorthands:

Term

django.utils.text.slugify

What you (may) want

New York City

new-york-city

nyc

Y Combinator

y-combinator

yc

Portland

portland

pdx

Texas

texas

tx

$

‘’ (empty)

usd, aud, etc?

US$

us

usd

A$

a

aud

bitcoin

bitcoin

btc

United States

united-states

usa

League of Legends

league-of-legends

league

Apple® iPod Touch

apple-ipod-touch

ipod-touch

Each website and niche has its own edge cases for slugs. So we need a solution that can scale, where you can craft your own functions.

How django-slugify-processor helps

This builds on top of django.utils.text.slugify to handle your django project’s edgecases. By default, django-slugify-processor will be a pass through to django’s default behavior. Adding slugification functions via your Django project’s settings file allows you to adjust.

Installation

$ pip install django-slugify-processor

Configure

To create a processor, create a function that accepts a string, and returns a string. Assume this is project/app/slugify_processors.py:

def my_processor(value):
   value = value.replace('++', 'pp')
   return value

Inside of your settings, add a SLUGIFY_PROCESSORS list of strings that points to the function. Anything that’s compatible with import_string, in your settings file:

SLUGIFY_PROCESSORS = [
   'project.app.slugify_processors.my_processor'
]

Usage

In normal django code

Import slugify from django_slugify_processor.text:

from django_slugify_processor.text import slugify

print(slugify('C++'))
> 'cpp'

Template code

django-slugify-processor is designed to override the built-in``slugify`` filter.

via load

You can load by default via {% load django_slugify_processor %} in your template.

In your settings INSTALLED_APPS:

INSTALLED_APPS = [
    'django_slugify_processor'
]

In your template:

{% load slugify_processor %}
{{"C++"|slugify}}

via built-in

To make this available in all templates, in the OPTIONS of your template engine, add django_slugify_processor.template_tags:

TEMPLATES = [{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'OPTIONS': {
        'builtins': [
            'django_slugify_processor.templatetags.slugify_processor',
        ],
    },
}]

From within the template file:

{{"C++"|slugify}}

Output should be: cpp

Models

For the most up to date documentation, view the documetation for the plugin you’re using (e.g. django-autoslug or django-extensions).

To use django-slugify-processor’s slugify instead of django’s default, there will be a field option to use the function.

django-extensions

Tested with 1.9.7 (2017-11-26):

from django.db import models

from django_extensions.db.fields import AutoSlugField
from django_slugify_processors.text import slugify

class MyModel(models.Model):
    title = models.CharField(max_length=255)
    slug = AutoSlugField(
        populate_from='title',
        slugify_function=slugify
    )

django-autoslug

Tested with 1.9.3 (2017-11-26):

from django.db import models

from autoslug import AutoSlugField
from django_slugify_processors.text import slugify

class MyModel(models.Model):
    title = models.CharField(max_length=255)
    slug = AutoSlugField(
        populate_from='title',
        slugify=slugify
    )

Credits

  • tox.ini based off DRF’s (BSD 2-clause licensed)

  • yapf configuration based off RTD / devel.tech’s (MIT-licensed)

Project details

python support

2.7, >= 3.6, pypy3

django support

3.0, >2, 1.11

Source

https://github.com/develtech/django-slugify-processor

Docs

https://django-slugify-processor.git-pull.com

API

https://django-slugify-processor.git-pull.com/api.html

Changelog

https://django-slugify-processor.git-pull.com/history.html

Issues

https://github.com/develtech/django-slugify-processor/issues

Test Coverage

https://codecov.io/gh/develtech/django-slugify-processor

pypi

https://pypi-hypernode.com/pypi/django-slugify-processor

Open Hub

https://www.openhub.net/p/django-slugify-processor

License

MIT

git repo

$ git clone https://github.com/develtech/django-slugify-processor.git

install stable

$ pip install django-slugify-processor

install dev

$ git clone https://github.com/develtech/django-slugify-processor.git
$ cd ./django-slugify-processor
$ pipenv install --dev --skip-lock
$ pipenv shell

tests

$ make test

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-slugify-processor-0.9.0b1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

django_slugify_processor-0.9.0b1-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-slugify-processor-0.9.0b1.tar.gz.

File metadata

  • Download URL: django-slugify-processor-0.9.0b1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.7.7 Linux/4.19.121-microsoft-standard

File hashes

Hashes for django-slugify-processor-0.9.0b1.tar.gz
Algorithm Hash digest
SHA256 c23547da1d24c7e020c997eb5b0779b627a7f66ae5aa9248b3a5f93d23af0564
MD5 34610dcaafc9cb9917c891d4d83187ba
BLAKE2b-256 319e2104fdcb8f9a69a33fe2c44f279cc98891eb69c15df2fb841b2ee3adf883

See more details on using hashes here.

File details

Details for the file django_slugify_processor-0.9.0b1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_slugify_processor-0.9.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 27f850f40b3d39ffc697e54b427908cf6a7cc0c7ab82964cf0fcbf144d6db714
MD5 1d762eecb45d6897ade5f01a82aa4be5
BLAKE2b-256 60abaff5da9956e31be3eda94342bf6315341dff4bd11456d275ba438a70eaaa

See more details on using hashes here.

Supported by

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