-1

I've created a form with flask. This is the python code that handles the requests that come from this form:

@app.route("/submitclips/", methods=['GET', 'POST'])
def submitclips():
   print(request)
   print(request.method)
   result = request.form.to_dict(flat=False)
   print(result)
   print(request.data)

HTML code:

<form action="/submitclips" method="POST">
<input type="hidden" name="taskpath" value="paththotask">
<table>
    <th>Video</th>
    <th>Mute</th>
    <th>Delete</th>
    <th>Keep</th>
    
    <tr>
        <td>
            <video width="320" height="240" controls>
              <source src="videosource.mp4" type="video/mp4">
            </video>
        </td>
        <td>
            <input type="radio" id="name$mute" name="name" value="mute">
        </td>
        <td>
            <input type="radio" id="name$delete" name="name" value="delete">
        </td>
        <td>
            <input type="radio" id="name$keep" name="name" value="keep" checked>
        </td>
    </tr>
</table>
<input type="submit">
</form>

This is a table and has more than one row, I changed the values to make it more readable. I guarantee that all names are unique where they are supposed to be unique though.

It has some code below it, but it crashes there because the form seems to be empty. I have checked the HTML and it's all correct, all inputs have a name. I also recorded the network traffic on google chrome to test this and I can see that all data in the form is present in the request data. So I'm pretty sure it's correct on the front-end. The problem is that it just doesn't show up in this method. In the code you can see several print statements and all of these show me empty data and not the data that I could see in my chrome browser. The method is GET, I don't know if it should be GET. In HTML I selected it to be a POST method, but if I remove the GET option from the python code, it gives me a "method not allowed" error.

I don't know what to do, this is some older code that used to work on my windows machine. I didn't change anything about it, but now I'm working on linux and it's completely broken, none of the forms work, it's all empty data.

DrRonne
  • 73
  • 8
  • Can you show the HTML part? It seems that you are POST-ing the form, which would make the form data print as empty. – Matt Dale Aug 21 '20 at 19:29
  • Also show your `return` command. If you are using the same template for the `form` and `return`, it will be a `GET` while the form loads. – GAEfan Aug 21 '20 at 19:43
  • I put the HTML code in there, the return statement is really just a redirect: return redirect("/") – DrRonne Aug 22 '20 at 09:17

2 Answers2

0

Here you have mentioned that the endpoint /submitclips has methods GET and POST. It means that whenever i hit the url "baseurl/submitclips" then this function will be executed. Inside the function you have not specified for which method you will have what response, i.e if i call the endpoint using GET or POST you will process it the same way. This is not a good practice. I suggest you put something like :

if request.method == 'POST' or if request.method == 'GET' to separate the execution based on the type of method.

Now coming to the HTML, you must have the HTML from where you are sending the request to the server. If that data is coming from a form, then as part of the form you can add two attributes,

<form method="post"> and <form action="/submitclips"> to specify that on submit of this form,you will be sending the form data through POST method to the "/submitclips" url. It will look like this.

<form method="post" action="/submitclips"">

For the Server side,

def submitclips():
   if request.method == 'POST' :
       print(request)
       print(request.method)
       result = request.form.to_dict(flat=False)
       print(result)
       print(request.data)

It should work after that.

Aditya
  • 53
  • 9
0

I have found the error. My HTML code would submit the form to "/submitclips" while the python code received requests from "/submitclips/". I don't know why this is wrong though, the tutorial that I followed for flask told me specifically that putting a slash at the end meant that it could receive requests from both "/submitclips" and "/submitclips/". This also worked earlier on my windows machine, but doesn't work anymore on my linux machine. I'm glad it's solved, but if anyone has an explanation for why this is wrong, feel free to answer.

DrRonne
  • 73
  • 8