Jacob Bennett
Jacob Bennett

Reputation: 67

How to replace url with link to that url in post with Flask?

How can I use Python and Flask to insert a link in a post if there is one.

For example if someone puts "google.com" in their post how can I then make that a link?

Upvotes: 2

Views: 1828

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

If you are using jinja2 as your templating system, you can use the built in urlize filter which will convert all urls in some text to html link tags:

{{ mytext|urlize(40, true) }}
links are shortened to 40 chars and defined with rel="nofollow"

It has numerous useful options you can read about in the docs:

http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters

If you are using some other templating engine, they probably have something similar.

If you want to do it client side there is a good discussion of all your options here:

How to replace plain URLs with links?

Upvotes: 4

Related Questions