Home > other >  how to create link in django for substring
how to create link in django for substring

Time:02-16

I am struggling to understand how to create a link in django's templates.

in my views.py file I have a list of lists (so a table). One of the fields may or may not contain links.

Views.py

#tableToView is a pd.DataFrame()

actualTable     = []
for i in range(tableToView.shape[0]):
   temp = tableToView.iloc[i]
   actualTable.append(dict(temp))

#Returning the 'actualTable' list of lists to be printed in table format in the template

return render(response, "manual/manualinputs.html", {'defaultDates':defaultDates, 'prevFilterIn':prevFilterIn, 'actualTable':actualTable, 'DbFailure':DbFailure})

so imagine my actualtable has a field 'News' that may or may not contain a link, e.g.: 'hello there, go to https://www.google.com/'

How do I manage the fact that I want in my template to have the same string printed out, but with the address actually being a link? I will also extend this to non-addresses (say twitter hashtags by parsing text). Should I act on views or templates?

I did try to go the views way: I substitute the

'hello there, go to https://www.google.com/'

with

'hello there, go to <a href="https://www.google.com/" target="_blank">https://www.google.com/</a>'

But I get the actual tags printed out, which is not what I want...

Any ideas?

CodePudding user response:

If you change it on the view, you need to print it with |safe on the template.

So it would be {{table.column|safe}}, this way your link will be a link and not a string

  • Related