Solaiman
Solaiman

Reputation: 318

Pass value from HTML to Python with Flask

So I am using Flask as micro framework and in one of my templates I am using the following Table:

<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> 

My question is about the following HTML code:

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

As you can see I am trying to pass a value to my Python code when a click is made on the input. The value is passed to my Python code, but now the value is visible on the front end, so it looks like this:

1svpTERHaYd-wSpFBRRRjp1TOj0H-FH4_66H2W1OLY Delete file

But I want it to be like this:

Delete file

In Python I am doing the following to extract the value:

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

I also tried something like this:

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

But I don't really know how I then can extract the name in my Python code, because I only need file['id'] to be passed and the value solution worked for me but that is not the ideal solution.

Upvotes: 0

Views: 4343

Answers (1)

MAZux
MAZux

Reputation: 981

Instead of POST try the GET method, like this:

<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>

If you want the POST method, you should send the file name via input with the hidden type.

<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>

In this case you'll get the file id like this:

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

BTW: most of your HTML isn't valid, you should really watch a tutorial about it.

Upvotes: 1

Related Questions