Does a digg url effect to a string, can be useful for using an item's title in the url,
from this:
.hi's., is (a) $ [test], will it "work"/ \
to this:
his_is_a_test_will_it_work
I understand this isn't a very well made script, I am not very good at string manipulation. But I would be happy if someone would recode it in a faster, more managable way. I recomend saving the rendering.
1 2 3 4 5 6 7 8 9 10 11 12 | def urlsafestring(string):
from string import strip, rstrip, lstrip, replace
string = str(string)
badchars = [".",",","+","=","%","#","!","@","$","^","*","(",")","\\","/","\"","[","]","{","}","|","'","?"]
for b in badchars:
string = replace(string, b, "")
string = rstrip(string)
string = lstrip(string)
string = replace(string, "-", "_")
string = replace(string, " ", "_")
string = replace(string, "__", "_")
return string
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
this might be better:
Example:
#
Isn't this essentially the same as Django's
slugify
filter?#
Please login first before commenting.