Django View : Pass value from checkbox to view



<input class="form-check-input" type="checkbox" value="{{}}" name = "chkbox" id="{{}}">

Value field should not be blank. If kept blank value is not passed to view post.

Input should be between the form tag.


Note : 

input on line 8 is kept outside the form. If kept in the form , when enter button is pressed form is submitted. Since this input is controlled by javascript , the is kept outside the form to prevent submit.

Only elements embedded between the form tag will be submitted to server.


<!-- Security Table-->
<h4> Porfolio</h4>
{% include "equity/partials/portfolio_list.html" %}

<h4> Security Listing</h4>
<div class = "row">
<input class="form-control col-6" type="text" placeholder="Search" onchange="fn_search(this.value)">
&nbsp; &nbsp;
<form action="/usersetting/portfolio/addselection/save/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="hidden" id="id_zselected" name="zselected" value="">
    <button type="submit" class="btn btn-warning">Warning</button>



<div  style="overflow-y: scroll;height:15em;">
<table class="table table-sm " >
        <th scope="col" style="width: 20%;">Symbol</th>
        <th scope="col" style="width: 30%;">Security</th>
        <th scope="col">Index</th>
          <th scope="col">Action</th>
        {% for zrec in zdata %}
      <tr id="c{{}}">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" value="{{}}" name = "chkbox" id="{{}}">
        <td >{{ zrec.symbol }}</td>
        <td class="zsecurity" id={{}}>{{ }}</td>
        <td> <a class="btn btn-link" href="/usersetting/security/delete/{{}}/" role="button">Delete </a></td>
        {% endfor %}

elif request.method == 'POST' and zaction == 'addselection' :
            # note checkbox is passed only when it is under form and value cannot be blank
            for zitem in list_of_input_ids :


zitem will return the value of the checkbox


<input type="checkbox" name="inputs" id="option{{}}" value={{}} />

if request.method == 'POST':
        #gives list of id of inputs