繁体   English   中英

如何在 django 中为 substring 创建链接

[英]how to create link in django for substring

我正在努力了解如何在 Django 的模板中创建链接。

在我的 views.py 文件中,我有一个列表列表(所以是一个表)。 其中一个字段可能包含也可能不包含链接。

视图.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})

所以想象一下我的actualtable有一个字段“新闻”,它可能包含也可能不包含链接,例如: 'hello there, go to https://www.google.com/'

我如何管理我希望在我的模板中打印出相同字符串但地址实际上是一个链接的事实? 我还将把它扩展到非地址(比如通过解析文本的 twitter 主题标签)。 我应该对视图或模板采取行动吗?

我确实尝试过 go 的观点方式:我用

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

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

但是我得到了打印出来的实际标签,这不是我想要的......

有任何想法吗?

如果在视图上更改它,则需要在模板上使用|safe打印它。

所以它将是{{table.column|safe}} ,这样你的链接将是一个链接而不是一个字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM