Login

slug_and_slash_to_dash - modified slugify for urls

Author:
bradmontgomery
Posted:
August 28, 2010
Language:
Python
Version:
1.2
Tags:
template filter slugify
Score:
0 (after 0 ratings)

This filter converts slashes to spaces in a a sting and then slugify's the result. However, it ignores leading and trailing slashes. For example, it can take something like this:

/some/url/with-an-existing-slug/

And turn it into this:

some-url-with-an-existing-slug

The filter was originally written to use the curent url as the disqus_identifier for Disqus comments. For example:

{{ request.META.PATH_INFO|slug_and_slash_to_dash }}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.template import Library
from django.template.defaultfilters import slugify, stringfilter

register = Library()

@register.filter
@stringfilter
def slug_and_slash_to_dash(value):
    """
    Converts any slashes in the given value into spaces, then slugify's the result.
    Leading and Trailing slashes (e.g. /some/url/) are ignored.
    """
    return slugify(' '.join(value.split('/')))

More like this

  1. Smart append slash middleware by akaihola 7 years, 5 months ago
  2. Automatically slugify slug fields in your models by Aliquip 8 years, 4 months ago
  3. Slugify alternative by exogen 8 years, 3 months ago
  4. Ensure submitted slugs do not conflict with existing resolvable URLs by ElfSternberg 6 years, 1 month ago
  5. Using the built-in slugify filter outside a template by jcroft 8 years, 5 months ago

Comments

Please login first before commenting.