Reputation: 5228
heyy there
i want to parse a text,let's name it 'post', and 'urlize' some strings if they contain a particular character, in a particular position.
my 'pseudocode' trial would look like that:
def urlize(post)
for string in post
if string icontains ('#')
url=(r'^searchn/$',
searchn,
name='news_searchn'),
then apply url to the string
return urlize(post)
i want the function to return to me the post with the urlized strings, where necessary (just like twitter does).
i don't understand: how can i parse a text, and search for certain strings? is there ok to make a function especially for 'urlizing' some strings? The function should return the entire post, no matter if it has such kind of strings. is there another way Django offers? Thank you
Upvotes: 2
Views: 1907
Reputation: 24823
First, I guess you mostly need this function in templates (you want to present the post "urlized").
There's the built-in template tag urlize, which is used like
{{ value|urlize }}
which takes a string value and returns it with links.
If you want to customize the logic, you can create your own template tag.
EDIT:
To find the # words you can use a simple regex search:
import re
a = "hello #world. foo #bar! #foo_bar"
re.findall("#(\w+)",a)
>> ['world', 'bar', 'foo_bar']
Upvotes: 4