I'm junior backend dev. I don't know how to use JS.
I can't set
<input type="checkbox" name="player_check">
true... or reverse i can't set
<input type="checkbox" name="player_check" checked>
false.
My code:
<table id="some_table">
<thead>
<tr>
<th>Prepared</th>
</tr>
</thead>
<tbody>
{% for player in players %}
<tr>
<td>
<input type="checkbox" name="player_check">
</td>
</tr>
{% endfor %}
</tbody>
</table>
Let's say in 'players' I have 5 players, each player has a value "player_check".
Two of them have:
player.player_check = True
Rest:
player.player_check = False
I'm trying to initiate checkbox in my table with these values using {{}} or {% %}
I've tried:
<input type="checkbox" name="player_check" value=1>
<input type="checkbox" name="player_check" value="1">
<input type="checkbox" name="player_check" value="True">
<input type="checkbox" name="player_check" value=True>
<input type="checkbox" name="player_check" value="checked">
Nothing works... Then i found that checkbox has a parametr checked so:
<input type="checkbox" name="player_check" checked>
That was ok BUT... now i cant turn it off:
<input type="checkbox" name="player_check" checked="false">
<input type="checkbox" name="player_check" checked="0">
<input type="checkbox" name="player_check" checked=0>
<input type="checkbox" name="player_check" checked="unchecked">
So i decided to use django templpates + change in python code: Now player.player_check equals checked or unchecked
It still doesn't work! Now i can't put {{ }} without name like "something"={{ foo }}
Now i have 0 ideas what i can do more to make it work... Maybe JS? but i cant believe there is no right way to do it just in django/python. ;/
POST["something_truthy"] is True if that checkbox was checked, and False if not.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
You can do this with for example an {% if ... %}
tag:
{% for player in players %}
<tr>
<td>
<input type="checkbox" name="player_check"{% if player.player_check %} checked{% endif %}>
</td>
</tr>
{% endfor %}
But I think you better use Django forms, which will handle this in a more elegant way for you: you specify the form field, and Django will handle the rest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With