PatrickHellman
PatrickHellman

Reputation: 79

request return empty dict from html

I am trying to get data and file from the following HTML template:

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload Example</title>
<style>
    body {
      margin: 0;
    }

    .center {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      font-size: 16px;
      
    }

  </style>
</head>
<body>
  <div class="center">
    <form method="POST" action="/upload" enctype="multipart/form-data">
        <table>
          <tr>
            <td class="lbl"><label for="sub_id">Subject Id:</label></td>
            <td><input class="fld" id="sub_id" type="text" placeholder="01" required></td>
          </tr>
          <tr>
            <td class="lbl"><label for="sub_first_name">First Name:</label></td>
            <td><input class="fld" id="sub_first_name" type="text" placeholder="John" required></td>
          </tr>
          <tr>
            <td class="lbl"><label for="sub_last_name">Last Name:</label></td>
            <td><input class="fld" id="sub_last_name" type="text" placeholder="Appleceed" required></td>
          </tr>
          <tr>
            <td class="lbl"><label for="files">File upload:</label></td>
            <td><input class="fld" name="file" type="file"></td>
          </tr>
        </table>
        <input type="submit" />
        <!--<button type='submit' name='selectionsubmit'>Submit</button>//-->
    </form>
  </div>
</body>
</html>

I am parsing this through flask @app.route:

@app.route("/upload", methods=["GET", "POST"])
def home():
    if request.method == "POST":
        file = request.files["file"]
        data = jsonify(request.form).json()
        print(data, flush=True)

when I try to print data, it returns an empty dict. How can I extract the data from HTML with the request, to get, for example, the value of sub_last_name with something like data.form['sub_last_name'] after form submit? Thanks

Upvotes: 2

Views: 175

Answers (1)

Detlef
Detlef

Reputation: 8572

The content of an input field is accessed via the name attribute of the specific element defined in the HTML template. The reason you only get the data of the file field is that you only defined this attribute for said input field.

To get the data of the other fields, specify an attribute name.

<input type="text" name="sub_last_name" />

You can then query the data as usual and known from the dict type within the endpoint. So you can also ask whether the respective key is included.

sub_last_name = request.form['sub_last_name']

If you are not sure whether the attribute will be passed, you want to specify a default value or convert the type on request, you can also use get. In this case no KeyError is raised, but None or the optional defined default value is returned if the name of the input field is not found.
The default return value is of type str. However, if you also pass the type attribute, the input will be converted as desired. If the type change fails, the default value is returned.

Upvotes: 1

Related Questions