Django模板中设置单选框checked属性时的解析问题
Django模板单选按钮预选中的语法错误修复
问题根源:Django模板的
{% if %}标签中,比较运算符前后必须加空格,你写的selected_value== choice.choiceid缺少了==前面的空格,导致模板解析器无法识别表达式。修正后的代码:
{% for choice in thisSQwithQ.qid.GetChoices %} <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.choiceid }}" {% if selected_value == choice.choiceid %} checked="checked" {% endif %} > <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> {% endfor %}
- 补充说明:Django模板语法对表达式格式要求严格,所有运算符(
==、!=、>、<等)前后都需要保留空格,否则会触发解析错误。
内容的提问来源于stack exchange,提问作者MEC
相关产品推荐
相关产品推荐

