6

I want to render a template.In the template, I have a checkbox called "IsValid", its value is from database. I want to read and edit the state of checkbox. After edit the checkbox, I want to store the its value in the database. How should I write the checkbox?

<div class="form-group">
    <label for="IsValid" class="control-label col-md-2">IsValid</label>       
<div class="col-md-2">
    <input type="checkbox" class="form-control" id="IsValid" name="IsValid" {{ checked="checked" if items[6]=1 else "" }} " >
</div>
</div>

In the code, items[6] is passed by view function.It's value is from database, it has two values,0 and 1.

How to modify {{ checked="checked" if items[6]=1 else "" }}?

Because it is wrong.

Wrong information:jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got '='

I don't know if you can understand because my English is poor.

1

1 Answer 1

9

Your jinja2 syntax is not correct. <input type="checkbox" class="form-control" id="IsValid" name="IsValid" {% if items[6]==1 %}checked{% else %}{% endif %}>

Sign up to request clarification or add additional context in comments.

2 Comments

The {% else %} is unnecessary here. {% if items[6]==1 %}checked{% endif %} suffices.
Thank you so much .I search this answer for long days. Now only I got it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.