Package: django-slugify-processor
django-slugify-processorCustom-slugify()support for django.What are slugs?Slugsare 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 aslugify()function which is also made available as adefault filter.Django slugs can be automatically generated in django models via packages such as:django-autoslug(docs,github)django-extensionsviaAutoSlugField(docs,github)The problemThis project is based on an article fromdevel.techcoveringdjango's import strings.Corner cases exist with slugification. For instance:Termdjango.utils.text.slugifyWhat you wantCc (correct)n/aC++ccppC#cc-sharpTo make matters worse, if using a specialized model field likeAutoSlugFieldfrom 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:Termdjango.utils.text.slugifyWhat you (may) wantNew York Citynew-york-citynycY Combinatory-combinatorycPortlandportlandpdxTexastexastx$'' (empty)usd, aud, etc?US$ususdA$aaudbitcoinbitcoinbtcUnited Statesunited-statesusaLeague of Legendsleague-of-legendsleagueApple® iPod Touchapple-ipod-touchipod-touchEach 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 helpsThis builds on top ofdjango.utils.text.slugifyto 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$pipinstalldjango-slugify-processorConfigureTo create a processor, create a function that accepts a string, and returns a string. Assume this isproject/app/slugify_processors.py:defmy_processor(value):value=value.replace('++','pp')returnvalueInside of your settings, add aSLUGIFY_PROCESSORSlist of strings that points to the function.
Anything that's compatible withimport_string,
in your settings file:SLUGIFY_PROCESSORS=['project.app.slugify_processors.my_processor']UsageIn normal django codeImportslugifyfromdjango_slugify_processor.text:fromdjango_slugify_processor.textimportslugifyprint(slugify('C++'))>'cpp'Template codedjango-slugify-processor is designed to override the built-inslugifyfilter.via loadYou can load by default via{% load django_slugify_processor %}in your template.In your settingsINSTALLED_APPS:INSTALLED_APPS=['django_slugify_processor']In your template:{%loadslugify_processor%}{{"C++"|slugify}}via built-inTo make this available in all templates, in theOPTIONSof your template engine, adddjango_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: cppModelsFor the most up to date documentation, view the documentation for the plugin you're using (e.g.
django-autoslug or django-extensions).To use django-slugify-processor'sslugifyinstead of django's default, there will be a field
option to use the function.django-extensionsTested with 1.9.7 (2017-11-26):fromdjango.dbimportmodelsfromdjango_extensions.db.fieldsimportAutoSlugFieldfromdjango_slugify_processors.textimportslugifyclassMyModel(models.Model):title=models.CharField(max_length=255)slug=AutoSlugField(populate_from='title',slugify_function=slugify)django-autoslugTested with 1.9.3 (2017-11-26):fromdjango.dbimportmodelsfromautoslugimportAutoSlugFieldfromdjango_slugify_processors.textimportslugifyclassMyModel(models.Model):title=models.CharField(max_length=255)slug=AutoSlugField(populate_from='title',slugify=slugify)Creditstox.ini based off DRF's (BSD 2-clause licensed)yapf configuration based off RTD / devel.tech's (MIT-licensed)Project detailspython support >= 3.8, pypy3django support > 3.2,Sourcehttps://github.com/tony/django-slugify-processorDocshttps://django-slugify-processor.git-pull.comAPIhttps://django-slugify-processor.git-pull.com/api.htmlChangeloghttps://django-slugify-processor.git-pull.com/history.htmlIssueshttps://github.com/tony/django-slugify-processor/issuesTest Coveragehttps://codecov.io/gh/tony/django-slugify-processorpypihttps://pypi.python.org/pypi/django-slugify-processorOpen Hubhttps://www.openhub.net/p/django-slugify-processorLicense MITgit repo$gitclonehttps://github.com/tony/django-slugify-processor.gitDevelopmentInstall stable:$pipinstalldjango-slugify-processorLocal installation:$gitclonehttps://github.com/tony/django-slugify-processor.git$cd./django-slugify-processor$poetryshell$poetryinstallTest:$maketest