0

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.

4
  • BTW.. I think, there is an error in your table structure. you haven't closed the <tr> tag inside <thead>. Commented Sep 8, 2018 at 13:10
  • 1
    @Arihant, it's wrongly closed after the loop. While invalid markup, most browsers will auto-close it when they meet</thead> tag and ignore the closing tag when they meet it later on. I'm not 100% sure though and it's definitely something that should be fixed. Commented Sep 8, 2018 at 13:14
  • @Arihant Ah I see it, thanks. Commented Sep 8, 2018 at 13:16
  • 1
    @AndreiGheorghiu I thought maybe using CSS could be also a kind of workaround. But I deleted the tag :) Commented Sep 8, 2018 at 13:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi thank you very much for your answer and explanation! Care to explain what aspects are incorrect in the HTML code? I am not really into HTML
Have a look at this: w3schools.com/html/html_xhtml.asp it's pretty helpful
You should also read about the deference between GET & POST http requests
Thanks for the information, will have a look at it. And I know the difference between both, in this case I needed to use a POST request for what I am trying to accomplish. Thanks for helping :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.