How to get a column value based on a row selected using the checkbox

I have a HTML table with checkboxes. I want to read the first column value based on the row selected using the checkbox. To read the values, a function is called after selecting the checkbox and clicking a button.

<a href="{% url 'app-analyze_retina' %}">
    <button onclick="" class="patient-add-btn button1"> Send for Analysis </button>
    {% if btn_clicked %}

            <table class="table" id="patient_list">
          <thead class="thead-dark">
              <th scope="col">Patient ID</th>
              <th scope="col">First Name</th>
              <th scope="col">Last Name</th>
              <th scope="col">Result/Severity Level</th>
              <th scope="col">Tested on</th>
              <th scope="col">AI confidence</th>
              <th scope="col">Comments</th>
            {% for patient in data %}
                <td bgcolor="mediumaquagreen">

                  <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input" name="checks" id={{patient.0}} value="{{patient.1}}">
                      <label class="custom-control-label" for={{patient.0}}>{{patient.1}}</label>
<!--                    <td  bgcolor="mediumaquagreen" > {{ patient.1 }}</td>-->
                    <td  bgcolor="mediumaquagreen">{{patient.2}}</td>
                    <td  bgcolor="mediumaquagreen" >{{patient.3}}</td>
                    <td  bgcolor="mediumaquagreen">2.846</td>
                    <td  bgcolor="mediumaquagreen">-</td>
                    <td  bgcolor="mediumaquagreen" >Cristina</td>
                    <td  bgcolor="mediumaquagreen" >913</td>

             {% endfor %}


    {% endif %}

Function to read the values

 def analyze_img(request):
        c = request.POST.getlist('checks')
        return render(request, 'workspace.html')

First I was trying to check whether I am able to read a checkbox but it returns an empty list.

My experience has been that things generally work better and easier with Django if you work with Django rather than trying to work around it.

In this case, this means that you’ll probably find it easier if you build your table as a Django form, probably with each row being an entry of a formset.

This is not to say that your general approach won’t work, or can’t be made to work - you’re just going to end up doing work that Django would already do for you.

1 Like

hi sir excuse me I have the same problem, did you solve it?