6

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. ;/

2
  • 2
    What's the reason you are not using a Django form, which will take care of this for you? Jul 13, 2018 at 8:14
  • It's old project from my company, and it was made like this. I took care of this project, mostly I'm learning and doing staff in backend API, but sometimes there are requirements to make something in frontend and for now it's a bit killing me :P, few more weeks and I hope that kind of easy problems won't appear Thanks for answer ;) Jul 13, 2018 at 8:29

2 Answers 2

13

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.

2
  • 1
    Thanks! Few minutes before you answered i found out how to do this but your proposal is much better I used if statement with one checkbox thats off and in else I wrote a code for second checkbox thats on. Now I have to change it ;) Thanks for answer! Jul 13, 2018 at 8:35
  • @AdrianKurzeja: well that would work as well :) but makes it less extendible. If you would later add other attributes to the <input> tag, it would mean that you need to specify those twice (and perhaps even more times). Jul 13, 2018 at 10:52
1

Another approach is to use the yesno filter.

  <input name="allowed" 
         type="checkbox" {{ player.player_check |yesno:"checked," }}>

https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#yesno

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.