python - flask: how to architect an POST endpoint that interfaces with HTML dropdowns -


so have sqlite db file i've used python script generate reading in present information flask endpoint.

the endpoint parses items db , generates dict object subsequently pass jinja template:

@app.route('/endpoint', method=['get', 'post']) def somefunction:     << generate finaldict via db queries >>     return render_template('whatever.html', **"finaldict": finaldict}) 

whatever.html renders bootstrap table, i.e.

<table>    <tr>       <th>          header 1       </th>       ...    </tr>    <tr>      <td>         {{ finaldict['someitem'] }}      </td>      ...    </tr> </table> 

so far, good, renders how i'm hoping might.

what want do, have final column in table contain dropdown menu allow user categorize item in row.

so if row looks id | name | whatever , have final | [dropdown] allow user select categorization, 1 of 3 possibilities.

what want know how structure html and, presumably, if request.method == 'post': catch in endpoint update database appropriate.

is possible treat entire table form object , parse through after user clicks submit button using above catch?

if not, what's best way structure things, given i'm looking here?

what want know how structure html

you'll want have simple html select 3 options:

<form name="category_form" id="category_form" action="/endpoint">     <select name='category' id='category'>         <option value='1'>option 1</option>         <option value='2'>option 2</option>         <option value='3'>option 3</option>     </select> </form> 

then use ajax post endpoint when dropdown selected. e.g., using jquery:

$(document).ready( function() {     var form = $('#category_form');      form.find('#category').change(function(){         $.ajax({             type: "post",             url: form.attr('action'),             data: form.serialize(),             success: function(response) {                 //optionally             }         });     }); }); 

presumably, if request.method == 'post': catch in endpoint update database appropriate.

that's how :-)

if request.method == 'post':     data = request.form     #do data 

data data form submitted endpoint.

check out docs on request object , how use it.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -