Python 如何在pandas表中构造有效的GET链接,然后在django视图中呈现为html
以下是我试图解决的任务:Python 如何在pandas表中构造有效的GET链接,然后在django视图中呈现为html,python,django,pandas,python-3.6,Python,Django,Pandas,Python 3.6,以下是我试图解决的任务: 将列添加到给定的pandas表,并向其添加一些链接 将表格呈现为html,并在django模板中显示表格 下面是代码(Python 3.6和Django 1.10.5): views.py def test(request): data = { 'Age': ['23', '35'], 'Name': ['Benny', 'Johny'], } table = pd.DataFrame(data) ta
def test(request):
data = {
'Age': ['23', '35'],
'Name': ['Benny', 'Johny'],
}
table = pd.DataFrame(data)
table.insert(loc=2, column='Link', value='')
number1 = 1234
number2 = 5678
# This is not working
link = f'<a href="/anotherview/?param1={number1}¶m2={number2}">Click Me</a>'
table.set_value(index=1, col='Link', value=link)
htmltable = table.to_html(index=True, border=1, na_rep='', justify='left', escape=False)
return render(request, 'test.html', {'table': htmltable})
def测试(请求):
数据={
‘年龄’:[‘23’、‘35’],
“姓名”:[“本尼”、“约翰尼”],
}
table=pd.DataFrame(数据)
表.插入(loc=2,column='Link',value='')
编号1=1234
数字2=5678
#这不起作用
链接=f''
表.设置值(索引=1,列=Link',值=Link)
htmltable=table.to_html(index=True,border=1,na_rep='',justify='left',escape=False)
返回呈现(请求'test.html',{'table':htmltable})
模板:test.html
<!DOCTYPE html>
<html lang="de">
<head>
</head>
<body>
{% autoescape off %}
{{ table }}
{% endautoescape %}
<!-- This is working -->
<a href="/anotherview/?param1=1234¶m2=5678">Click Me</a>
</body>
</html>
{%autoescape off%}
{{table}}
{%endautoescape%}
问题是链接没有正确渲染。以下是呈现的HTML源代码的图片:
当我把链接直接放到HTML中时,它就工作了。我很确定这与djangos或pandas的自动转义或某种以错误的方式屏蔽某些字符(“&”)有关,但我不知道如何正确执行。谁有建议?您应该尝试下面发布的解决方案:
pd.set_option('display.max_colwidth', -1)