2

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?

Wayne Werner
  • 45,646
  • 26
  • 189
  • 275
Jacob Bennett
  • 67
  • 1
  • 7

1 Answers1

4

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?

Community
  • 1
  • 1
Robert Moskal
  • 20,867
  • 7
  • 59
  • 83