Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Jinja2 generated input value data?

In my HTML file, I have:

  <table>
      {% for user in user_data_html %}
      <tr>
         <td>
            <input id="firstname" name="firstname" type="text" value='{{ user.FirstName }}' />  
         </td>
         <td>
             <input name="submit" type="submit" value='update' />
         </td>
     </tr>
     {% else %}
     <tr><td>no user found</td></tr>
     {% endfor %}
 </table>

I want to modify the user name in the webpage by clicking update button in each row. But I always get the first "firstname" using the following python code in the backend:

firstname = request.form['firstname']

How can I solve this problem?

like image 938
Tianyun Ling Avatar asked Jun 03 '14 19:06

Tianyun Ling


1 Answers

Forms get confused when you use the same name for each input name. You could either create a separate form around each table cell with the first name or you can use the jinja2 loop index to create unique input names...

<input id="firstname{{ loop.index }}" name="firstname{{ loop.index }}" type="text" value='{{ user.FirstName }}' />

Hope this helps!

like image 56
Andrew Kloos Avatar answered Sep 24 '22 19:09

Andrew Kloos