【问题标题】:Could not parse the remainder: '==obj.id' from 'sobj.id==obj.id'无法解析剩余部分:来自 'sobj.id==obj.id' 的 '==obj.id'
【发布时间】:2020-07-09 09:44:09
【问题描述】:

我在 django 模板中使用嵌套的 for 循环来打印我从数据库中获取的数据

            {% if objectives %}
            {% for obj in objectives %}
            {% for sobj in sobjective %}
            {% if sobj.id==obj.id %} yes {% endif %}

所有打开的标签都关闭了

但它会引发错误 正如我上面提到的

TemplateSyntaxError at /objectives
Could not parse the remainder: '==obj.id' from 'sobj.id==obj.id'

【问题讨论】:

  • ==周围添加空格。
  • 你看过in this question提到的解决方案了吗?错误可能不在您提供的代码 sn-p 中。

标签: python django python-3.x django-templates


【解决方案1】:

Django 模板语言要求== 运算符周围有空格,因此您应该将其实现为:

{% if <b>sobj.id == obj.id</b> %} yes {% endif %}

在 Python 中x==y 是有效的,但 Django 的模板语言有特定的标记和语法。

【讨论】:

  • 非常感谢您的帮助我有另一个关于范围的问题我在 try: tage 中使用变量但是当我在“try”之外创建字典时 django 引发了一个错误,即局部变量引用在分配之前,我尝试通过编写全局来更改范围,但顺便说一下,它并没有解决我的错误。你有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2021-04-15
  • 2017-09-11
相关资源
最近更新 更多