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

使用Django Templates报错:未闭合with标签,求解决方法

解决Django Templates中with标签未闭合的错误

错误原因

出现Unclosed tag on line 6: 'with'. Looking for one of: endwith.错误的核心原因是:Django模板的{% with %}属于块级标签,必须使用对应的{% endwith %}标签闭合,你的代码仅添加了起始标签,遗漏了闭合步骤。

修正后的完整代码

<!DOCTYPE html>
<html>
  <body>
    <h1>Favorite rapper:</h1>

    {% with person="2pac" %}
      <h1>{{person}}</h1>
    {% endwith %}
  </body>
</html>

补充说明

Django模板中所有块级标签(如if、for、with等)都遵循“成对使用”的规则,必须有对应的结束标签来包裹代码块,否则模板引擎无法正确解析内容。

内容的提问来源于stack exchange,提问作者Martin Arreola

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 19:40:40