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

Flask应用出现TemplateSyntaxError,模板继承语法报错求助

Fixing TemplateSyntaxError in Your Flask Template

Let's break down the issues in your code and fix them step by step:

1. Invalid Jinja2 Template Tag Syntax (The Direct Cause of the Error)

Your extends tag has two syntax mistakes that are triggering the TemplateSyntaxError:

  • You closed it with a single } instead of the required %}. Jinja2 uses {% ... %} for block tags, so the closing syntax must match exactly.
  • While not strictly mandatory, adding spaces around extends (like {% extends "layout.html" %}) follows Jinja2's best practices and avoids edge-case parsing issues.

Original broken line:

{%extends "layout.html"}

Fixed line:

{% extends "layout.html" %}

2. Typo in Static Resource Path (Bonus Fix)

Looking at your truncated image tag, you wrote statkc/im... — that's a clear typo for static, Flask's default directory for static files. To make this more robust (and avoid future path issues), use Flask's url_for helper to reference static files instead of hardcoding the path:

Original (truncated) line:

<img src="statkc/im...

Fixed line using url_for:

<img src="{{ url_for('static', filename='images/your-device-image.jpg') }}" alt="Device preview">

Full Corrected Template Code

Here's the complete fixed version of your template:

{% extends "layout.html" %} 
{% block content %} 
<main class="hero-section">
  <div class="container">
    <div class="section-content">
      <h2> Discover places near you. </h2>
      <a href="#" class="btn-primary">Sign up</a>
      <a href="#" class="btn-secondary">Learn more</a>
    </div>
    <div class="section-device">
      <img src="{{ url_for('static', filename='images/your-device-image.jpg') }}" alt="Device preview">
    </div>
  </div>
</main>
{% endblock content %}

Quick Reminders

  • Jinja2 is strict about tag syntax — always double-check that block tags ({% ... %}) start with {% and end with %}.
  • Using url_for for static files ensures your paths work correctly even if you deploy your app to a subdirectory or adjust your static folder configuration later.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:33:14