API Reference

Slugify function

django_slugify_processor.text.slugify(value, allow_unicode=False)[source]

Override default slugify in django to handle custom scenarios.

Run value through functions declared in SLUGIFY_PROCESSORS setting. The value is then passed-through to Django’s django.utils.text.slugify().

Return type:

str

Parameters:
  • value (str) – A value such as an article name or page title, to “slugify”, (turn into a clean, URL-friendly short name)

  • allow_unicode (bool) – Whether or not to allow unicode (e.g. chinese).

Returns:

Clean, URL-friendly short name (a.k.a. “slug”) of a string (e.g. a page or article name).

Return type:

str

Examples

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)[source]

Template filter override for Django’s django.utils.text.slugify().

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

Return type:

str

Parameters:

value (str) – A value such as an article name or page title, to “slugify”, (turn into a clean, URL-friendly short name)

Returns:

Clean, URL-friendly short name (a.k.a. “slug”) of a string (e.g. a page or article name).

Return type:

str

Examples

Usage in a Django template:

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