API Reference

Slugify function

django_slugify_processor.text.slugify(value, allow_unicode=False)

Override default slugify in django to handle custom scenarios.

Run value through functions declared in SLUGIFY_PROCESSORS. The value is then passed-through to django’s slugify.

Parameters:
  • value (string) – string to slugify
  • allow_unicode (bool) – whether or not to allow unicode (e.g. chinese)

Examples of slugify processors, assume project/app/slugify_processors.py:

def slugify_programming_languages(value):
    value = value.lower()

    value = value.replace('c++', 'cpp')
    value = value.replace('c#', 'c-sharp')
    return value

def slugify_geo_acronyms(value):
    value = value.lower()

    value = value.replace('New York City', 'nyc')
    value = value.replace('United States', 'usa')
    return value

def slugify_us_currency(value):
    value = value.lower()

    value = value.replace('$', 'usd')
    value = value.replace('US$', 'usd')
    value = value.replace('US Dollar', 'usd')
    value = value.replace('U.S. Dollar', 'usd')
    return value

Settings:

SLUGIFY_PROCESSORS = [
    'project.app.slugify_programming_languages',
    'project.app.slugify_geo_acronyms',
    'project.app.slugify_us_currency',
]

Template tag

django_slugify_processor.templatetags.slugify_processor.slugify(value)

Template filter intended to override django 1.11+’s default slugify.

This can be installed via a builtin, or via {% load slugify_processor %}.

Usage in a Django template:

{% load slugify_processor %}  {# unless you added it to builtins %}
{{variable|slugify}}  {# assuming "variable" is in context %}
{{"C++"|slugify}}