[英]Pass value from HTML to Python with Flask
所以我使用 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']
並且值解決方案對我有用,但這不是理想的解決方案。
而不是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 都是無效的,你真的應該看一個關於它的教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.