2

I'm trying to submit a form through post in FastAPI and get a form variable that is stored as a list. When I read back the value all I get is an empty list. There are several variables that will eventually be in list form but all I am trying to get at the moment is the array that holds the quantity values.

What am I doing wrong?

Here is my code:

Python Code

@app.post("/edit_bom")
def edit_bom(board_id: int = Form(...), quantity: list = Form(...)):
    print("Editting BOM, board ID: " + str(board_id))
    print("quantity: ", quantity)
    
    return RedirectResponse(url=f"/get_bom_details/" + str(board_id), status_code=303)

HTML Code

<form method="post" action="/edit_bom" id="parent-form">

  <div class="mb-3 row">
    <div class="d-grid gap-2 col-2">
      <button type="button" class="btn btn-outline-primary add-one-more"><i class="fas fa-fw fa-plus-square"></i> New Row</button>
    </div>

    <div class="d-grid gap-2 col-2">
      <button type="submit" name="action" value="save-bom" class="btn btn-outline-success"><i class="fas fa-fw fa-check"></i> Save</button>
    </div>
  </div>

  <div class="mb-3 row" id="header">
    <label class="col-form-label col-sm-1 text-sm-end"></label>
    <div class="col-sm-7">
      <h3>Part Number</h3>
    </div>
    <div class="col-sm-2">
      <h3>Quantity</h3>
    </div>
    <div class="input-group col">
      <h3>Edit</h3>
    </div>
    <div class="input-group col">
      <h3>Delete</h3>
    </div>
  </div>
  <div class="mb-3 row bom-line-item copy-fields" id="entry">
    <label class="col-form-label col-sm-1 text-sm-end child-row-label">1</label>
    <div class="col-sm-7 child-outer">
      <input type="text" name="part-name_1" id="0" class="form-control child-part-name" placeholder="Board Number" value="ISL73847SEHDEMO1ZB">
    </div>
    <div class="col-sm-2 child-outer">
      <input type="number" name="quantity[]" id="0" class="form-control child-part-quantity" placeholder="Enter quantity" value="1">
    </div>
    <div class="input-group col child-outer">
      <button id="btn-edit_1" class="btn btn-primary child-btn-edit" type="button"><i class="fas fa-fw fa-edit"></i></button>
    </div>
    <div class="input-group col child-outer">
      <button id="btn-del_1" class="btn btn-danger child-btn-delete" type="button"><i class="fas fa-fw fa-ban"></i></button>
    </div>
  </div>
  
  <div class="end-of-list" name="noOfRows" value="1"></div>
  <input type="hidden" name="board_id" value="1">
</form>

My output in the command prompt:

Editting BOM, board ID: 1
quantity:  []
2
  • 4
    name="quantity", not name="quantity[]" Commented Jul 1, 2022 at 20:36
  • Well I'll be damned... that was it! Thank you! Commented Jul 2, 2022 at 14:07

1 Answer 1

2

Using the comment from @MatsLindh

I had to change the HTML element name from "quantity[]" to "quantity" and it works now.

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

Comments

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.