1

I have a table and in the table a form with fields the user can change. Three of these fields use check boxes. I'm able to reflect what is currently in the database after following this post, but I haven't been able to find something else for the next piece of the puzzle.

The problem I'm having is that if a checkbox is set to true(checked) and then I unselect it, the POST data does not include anything that says that it is not checked anymore. It doesn't give a value of "off", "0" or false. How can I make sure that when the boxes are unchecked I still get those values("off", 0" or "false")? on the post data?

Below is my code:

<form id=result_form action= "{% url 'example:save_notes' %}" method="post">
{% csrf_token %}
    <table id="results_table" class="formdata" summary="Example Search Results"
    data-order='[[ 1, "des" ]]' data-page-length='25'>
        <thead>
            <tr>
                <th style="width:5%" scope="col">Search Method</th> 
                <th style="width:20%" scope="col">Search Date</th>
                <th style="width:5%" scope="col">City</th>
                <th style="width:10%" scope="col">Example Name</th>
                <th style="width:10%" scope="col">Article Title</th>
                <th style="width:10%" scope="col">Article Link</th>
                <th style="width:5%" scope="col">Reviewed </th>
                <th style="width:5%" scope="col">J/J Related</th>
                <th style="width:5%" scope="col">N Related</th>
                <th scope="col">General Notes</th>
                <th scope="col">Findings</th>
                <th scope="col">Edit</th>

            </tr>
        </thead>
    <tbody>
    {% for result in results %}
        <tr>
            <td>{{result.notes.method}}</td>
            <td>{{result.retrieved_on}}</td>
            <td>{% for e in result.search_result_t.all%}{{e.search_term_id.example_abbrev}}&nbsp{% endfor %}</td>
            <td>{% for e in result.search_result_t.all%}{{e.search_term_id.example_ID.example_name}}{% endfor %}</td>
            <td>{{result.title}}</td>
            <td width="5%"><a href={{result.link}} target="_blank">{{result.link}}</a></td>
            <form action="{% url 'example:form_edit' %}" method="POST">
            <td><input type="checkbox" name="reviewed" {% if result.notes.reviewed %}checked{% endif %}></td>
            <td><input type="checkbox" name="jj" {%if result.notes.j_j %}checked{% endif %}></td>
            <td><input type="checkbox" name="n" {%if result.notes.n_related %}checked{% endif %}></td>
            <td><textarea cols="40" rows="15" name="general_notes" value = "{{result.notes.general_notes}}">{{result.notes.general_notes | default_if_none:""}}</textarea>
            <td><textarea cols="40" rows="15" name="significant_findings" value = "{{result.notes.significant_findings}}">{{result.notes.significant_findings | default_if_none:""}}</textarea>
            <td><button>Save</button></td>    
            <input type="hidden" name="result.id" value={{result.id}}>
            </form>
        </tr>
        {% endfor %}
L. P.
  • 165
  • 1
  • 19

2 Answers2

3

Unchecked boxes are not part of the request - see here. But why do you need this information as you should know what checkboxes you have sent.

ger.s.brett
  • 3,267
  • 2
  • 24
  • 30
  • It only sends the checkboxes if they are checked. I'm guessing I have to create something in the views.py along the lines of if reviewed, jj and n are not present in the POST then they are false. – L. P. Apr 09 '19 at 15:05
  • For this you would need to show the view code that you have so far. Can you add it into the question? – ger.s.brett Apr 09 '19 at 15:12
2

Thank you for your guidance ger.s.brett. I used the answer to this post to solve my problem.

This looks to see if the field is in the POST:

reviewed = request.POST.get('reviewed', "false")

If the field is present then it uses the POST value. If it is not present it sees it as false.

L. P.
  • 165
  • 1
  • 19