You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 14:52:06