你忘了在Django中注册或加载这个标签吗?

2024-04-30 04:29:55 发布

您现在位置:Python中文网/ 问答频道 /正文

在我正在工作的django项目中,当我将{% set alpha = SafeExec.objects.get(testcase=a_test) %}添加到html中时,它显示了这个错误。如何摆脱它?在

这是我的模板代码:

{% for a_testcases in testcases %}
    <li><i>{{ a_testcases.0.program.name }}</i> <br/>
    {% for a_test in a_testcases %}
        {% set alpha = SafeExec.objects.get(testcase=a_test) %}
            {{ alpha.cpu_time }}
    {% endfor %}
    <input id="id{{ a_test.id }}" type="checkbox" name="testcases_cbx" value="{{ a_test.id }}" checked/>
    <label style="display: inline" for="id{{ a_test.id }}">{{ a_test.name }}</label> <br/>
{% endfor %}

这是错误的屏幕截图:Template syntax error. Did you for get to load this tag?


Tags: nameintestbralphaidforget
2条回答

您不能在模板中执行任何操作。set不是模板标记;SafeExec在上下文中不存在;而且无论如何也不能用模板中的参数调用方法。在

在没有看到您的模型的情况下,我无法确切地告诉您需要做什么,但是看起来testcase有一个SafeExec的外键,所以您应该这样做:

{{ a_test.safeexec.cpu_time }}

如果要在django模板中创建任何变量,请使用“with”标记。在

检查这个答案

How to set a value of a variable inside a template code?

相关问题 更多 >