使用Flask将值从HTML传递到Python

问题描述 投票:-1回答:1

所以我使用Flask作为微框架,在我的一个模板中,我使用下表:

<table id = "productentabel" width = "auto" class="table table-striped b-t b-b">
<thead>
<tr class = "header">
<th>Name</th>
<th>Url</th>
</thead>
{% for file in all_files['files'] %}
{% if file['mimeType'] == 'application/vnd.google-apps.document'  %}
<TR>
<TD  width="auto" >{{file['name']}}</TD> 
<td><a class="btn btn-xs white" href = "https://docs.google.com/document/d/{{file['id']}}" target ="_blank">Go to file</a></td>
<td><form method=POST action="delete_file"><input type=submit name="delete" value ="{{file['id']}}" class="btn btn-xs white">Delete file</input>
</form></td>
</TR>
{% endif %}
{% endfor %}
</tr> 
</table> 

我的问题是关于以下HTML代码:

<form method=POST action="delete_file"><input type=submit name="delete" value ="{{file['id']}}" class="btn btn-xs white">Delete file</input>
</form>

正如您所看到的那样,当我在输入上单击时,我试图将值传递给我的Python代码。该值传递给我的Python代码,但现在该值在前端可见,所以它看起来像这样:

1svpTERHaYd-wSpFBRRRjp1TOj0H-FH4_66H2W1OLY删除文件

但我希望它是这样的:

删除文件

在Python中,我正在执行以下操作来提取值:

fileid = request.form.get('delete')

我也尝试过这样的事情:

<form method=POST action="delete_file"><input type=submit name="{{file['id']" class="btn btn-xs white">Delete file</input>
    </form>

但我真的不知道如何在我的Python代码中提取名称,因为我只需要传递file['id']并且值解决方案对我有效,但这不是理想的解决方案。

python html flask jinja2
1个回答
1
投票

而不是POST尝试GET方法,像这样:

<td><form method="get" action="delete_file?file_name={{file['id']}}"><input type="submit" name="delete" value ="Delete file" class="btn btn-xs white"/></td>

如果你想要POST方法,你应该通过hidden类型的输入发送文件名。

<td><form method="post" action="delete_file"><input type="submit" name="delete" value ="Delete file" class="btn btn-xs white"/><input type="hidden" name="file_id" value="{{file['id']}}" /></td>

在这种情况下,您将获得如下文件ID:

fileid = request.form.get('file_id')

顺便说一下:你的大部分HTML都无效,你应该真正看一下它的教程。

© www.soinside.com 2019 - 2024. All rights reserved.