mercredi 24 avril 2019

request.form.getlist() not working with data from AJAX request

I'm having issues with checkboxes sharing the same name in Flask. Specifically, the form has a checkbox for each project, and I need to get a list of project ID's for each project that the user checked in the form. In the HTML, each project has a checkbox with name = 'project' and value = the project ID. Then, I try to extract a list of project id's using 'request.form.getlist('project') on the server side. However, all it gives me is an empty list.

HTML:

<form id = 'filterForm' action="" method='post' >
  <fieldset>
    <legend>Projects</legend>
      <input type="checkbox" name="project" value="1">Project A<br>
      <input type="checkbox" name="project" value="2">Project B<br
      <input type="checkbox" name="project" value="3">Project C<br
  </fieldset>
  <button type = 'button' class = 'ui small blue button' id = 'applyFilters'>Apply</button>
</form>

Flask:

@user_web.route('/get_admin_table', methods=['POST'])
@Auth.auth_required
def get_admin_table():
    projects = request.form.getlist('project')
    print(projects) #this returns an empty list.
    #...some other stuff

I've run print(request.form) to make sure the request was going through, and I get the following:

ImmutableMultiDict([('form[0][name]', 'project'), ('form[0][value]', '2'), ('form[1][name]', 'project'), ('form[1][value]', '1')])




Aucun commentaire:

Enregistrer un commentaire