slugify js -> python

 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. Slugify alternative by exogen 7 years ago
  2. Using the built-in slugify filter outside a template by jcroft 7 years, 1 month ago
  3. Built-in Slugify with filtering. by jcrawford 5 years ago
  4. slugify with transliteration by jezdez 3 years, 5 months ago
  5. Automate unique slugs by taojian 6 years, 4 months 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
Doh!

@everyone else
yep, this one's better

Ha!
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++
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.
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', ....

#

(Forgotten your password?)