samedi 4 juillet 2015

In django, how to get the records from HTML table using checkboxes?

This is the view function:

def main_view(request):
    x=request.POST.getlist('checks')
    print x

    return render(request, 'main.html')

This is main.html

<form role="form" action="/main/" method="post">{% csrf_token %}
  <table class="table">
    <thead>
      <tr>
        <th>Heading 1</th>
        <th>Heading 2</th>
        <th>Heading 3</th>
      </tr>
    </thead>
    <tbody>
      <tr class="success">
        <td><input type="checkbox" name="checks" id="1" />data11</td>
        <td>data12</td>
        <td>data13</td>
      </tr>
      <tr class="success">
        <td><input type="checkbox" name="checks" id="1" />data21</td>
        <td>data22</td>
        <td>data23</td>
      </tr>
      <tr class="success">
        <td><input type="checkbox" name="checks" id="1" />data31</td>
        <td>data32</td>
        <td>data33</td>
      </tr>
    </tbody>
  </table>
  <button type="submit" class="btn btn-default btn-success pull-right">Remove</button>
</form>

When I run the app, on the console the output of print statement (in main_view) is

[u'on', u'on']

Basically, what I'm trying to do is, user should be able to select entries from HTML table and when he/she clicks on "Remove" button, the entries should be removed. I don't know how to get the information about selected entries from the request object in my view. How can I handle this in my view?




Aucun commentaire:

Enregistrer un commentaire