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
|
Comments
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?
#
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.
#
Django's default slugify doesn't remove the words such as "a", "an" etc. but this does. Nice useful stuff - well done.
#
@jcroft Doh!
@everyone else yep, this one's better
Ha! funny how that worked out =)
#
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.
Ahh, that is indeed a benefit. Cool.
#
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.#
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.
#
Please see Snippet 43 for an example of using the built-in slugify filter in a Python script. To everyone who says it can't be used "anywhere," you're wrong. I do it all the time:
http://www.djangosnippets.org/snippets/43/
#
@jcroft
Whoops! my "Doh++" comment was written seconds after your comment which explained that defaultfilters.* are just python functions, had I read that first (or researched it myself) I wouldn't have posted my Doh++ comment.
Thanks for your clarification and as per DRY principles, maybe this snippet should simply be removed.
#
@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', ....
#