1

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?

1 Answer 1

5

You need to give your checkboxes a value attribute corresponding to the ID of the record.

<td><input type="checkbox" name="checks" id="1" value="data31" />data31</td>

Note that a) the readable value should be a label, to improve accessibility, and b) you probably want to output both value and label with a template variable from the record itself.

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

1 Comment

Yes, this worked! Now I can see in the list ["data11", "data31"], etc..instead of ["on", "on"]. This is exactly what I wanted. Thank you very much Daniel :)

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.