【问题标题】:Could not parse remainder error while using ternary operator?使用三元运算符时无法解析余数错误?
【发布时间】:2021-04-21 20:59:59
【问题描述】:

users.html

<td><div class="form-group">
                         <div class="custom-control custom-switch">
                           <input type="checkbox" class="custom-control-input" id="user-{{i.id}}" {{(i.status) ? 'checked' : ' '}}
                              onclick="changeUserStatus(event.target, {{ i.id }});">
                           <label class="custom-control-label pointer"></label>
                        </div>
                     </div>
                     </td>

为什么会出现这个错误?

Could not parse the remainder: '(i.status=='True') ? 'checked' : ''' from '(i.status=='True') ? 'checked' : '''

任何帮助将不胜感激?

【问题讨论】:

    标签: django django-errors


    【解决方案1】:

    这不是一个有效的语法。 Django 模板没有三元运算符。您可以使用 {% if %} 和 {% else %}

    【讨论】:

    • 您也可以使用内置标签过滤器 yesno ,这类似于一行中的 if else 语句,考虑到过滤器接受三个参数“真,假,也许”,第三个使用对于无值
    【解决方案2】:

    您在 python 和 javascript 语法之间感到困惑,id="user-{{i.id}}" {{(i.status) ? 'checked' : ' '}} 这是一个 javascript 语法,例如在 reactjs 中工作,

    对于 django 模板,您可以使用 yesno 内置标签过滤器

               <td>
                  <div class="form-group">
                       <div class="custom-control custom-switch">
                         <input type="checkbox" class="custom-control-input" id="user-{{i.id}}" {{ i.status |yesno:"checked,," }}
                            onclick="changeUserStatus(event.target, {{ i.id }});">
                         <label class="custom-control-label pointer"></label>
                      </div>
                  </div>
                </td>
    

    查看标签documentation

    【讨论】:

      猜你喜欢
      • 2011-09-10
      • 2013-12-22
      • 2015-05-17
      • 2012-11-19
      • 2020-10-30
      • 2012-12-18
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多