django form: how to check out checkbox state in template

前端 未结 4 1698
情话喂你
情话喂你 2021-02-06 14:46

I have a form with checkboxes that works fine, but when the user submits the form with errors and my checkbox is checked I need to change div class which holds the checkbox. So

4条回答
  •  走了就别回头了
    2021-02-06 15:11

    Use {% if form.mycheckbox.value %}. This will evaluate to true if the box is checked. For the opposite behavior, use {% if not form.mycheckbox.value %}.

    Note the syntax is {% if ... %}, not {{ if ...}}. Percent-brackets are for commands, double-brackets are for outputting variables.

提交回复
热议问题