Login

slug_and_slash_to_dash - modified slugify for urls

Author:
bradmontgomery
Posted:
August 28, 2010
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.