Login

slugify js -> python

Author:
santuri
Posted:
February 26, 2007
Language:
Python
Version:
Pre .96
Score:
2 (after 4 ratings)

This code is derived from the slugify JS function used in django's admin interface. It will create django-compatible slugs for you. Sometimes I do batch imports and need my items to have slugs, I give this script the item's title and get a slug back. Simple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#######################
#
# Create slugs, derived from django's JS implementation
#
# name = "This IS A boOk's TiTle"
# slug = slugify(name)
#
# >>> print slug
# 'this-is-a-books-title'
#
#######################
import re

def slugify(inStr):
    removelist = ["a", "an", "as", "at", "before", "but", "by", "for","from","is", "in", "into", "like", "of", "off", "on", "onto","per","since", "than", "the", "this", "that", "to", "up", "via","with"];
    for a in removelist:
        aslug = re.sub(r'\b'+a+r'\b','',inStr)
    aslug = re.sub('[^\w\s-]', '', aslug).strip().lower()
    aslug = re.sub('\s+', '-', aslug)
    return aslug

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

jcroft (on February 27, 2007):

Hmm...that's cool, but when I need to do that, I usually just use the built-in slugify function from django.template.defaultfilters. Is this one better, somehow?

#

ericflo (on February 27, 2007):

I think that the added value here is that it's a python function that can be applied anywhere in your code.

An example that I'm thinking of is that you could slugify some text and save it in the database as a field instead of slugifying it on display. It could save some CPU cycles that way.

#

obeattie (on February 27, 2007):

Django's default slugify doesn't remove the words such as "a", "an" etc. but this does. Nice useful stuff - well done.

#

santuri (on February 27, 2007):

@jcroft<br /> Doh!

@everyone else<br /> yep, this one's better

Ha!<br /> funny how that worked out =)

#

jcroft (on February 27, 2007):

I think that the added value here is that it's a python function that can be applied anywhere in your code.

So can django.template.defaultfilters.slugify. It's just a Python function, that can be applied anywhere, just like this one. I don't see the difference in that regard. I use it all over my Python scripts.

Nothing about Django's built-in function is display-only. Template filters don't have to be used only in templates.

Django's default slugify doesn't remove the words such as "a", "an" etc. but this does.

Ahh, that is indeed a benefit. Cool.

#

santuri (on February 27, 2007):

Doh++<br /> Yes, as ericflo said, the added value is that the function can be used anywhere.

In one project I have a script that parses a huge tab delimited file of books and uses django's orm to populate the database based on the file. <br /> Each line in the file has a book title that needs to have the url:

/author-slug/book-slug/

so I run:

slugify(book.title)

to produce a slug for each book title that I save in the database.

#

jcroft (on February 27, 2007):

Like I said, the built-in filter can be used anywhere, too. Just import it and use it. I don't understand the difference, I guess.

I use the built-in slugify filter, for example, to create slugs for events I pull from upcoming.org's API.

#

jehiah (on February 21, 2008):

@jcroft,others

test the function carefully, and look at the example in the code. the way it's written it WILL NOT actually strip out 'is', 'an', ....

#

Please login first before commenting.