- 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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
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<br /> Doh!
@everyone else<br /> yep, this one's better
Ha!<br /> 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++<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.
#
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.
#
@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.