jinja2.exceptions.TemplateSyntaxError:预期标记“语句块结尾”,得到了“ show_jobs”

时间:2019-01-12 20:37:45

标签: python html macros syntax-error jinja2

我刚刚开始学习Python,但是我一直被这个错误困扰。我正在做教程,由于某种原因我找不到此错误的原因。

有什么想法吗?以下是找到“ show_jobs”的脚本:

index.html

{% extends "layout.html" %}
<!doctype html>
{% block content %}
  <div class="columns">
    <div class="column">
      <h1>Jobs</h1>
    </div>
    <div class="column is-one-fifth">
    </div>
  </div>
  {{ show_jobs(jobs) }}
{% endblock %}

layout.html

{% from '_macros.html' import show_job, import  with context %}
<!doctype html>
<html>
  <head>
    <title>Job Board</title>
    <link rel="stylesheet" href="{{ url_for('static',
    filename='css/bulma.min.css')}}">
    <link rel="stylesheet" href="{{ url_for('static',
    filename='css/app.css')}}">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css">
  </head>
  <body>
    <div class="container top">
      <div class="content">
        {% block content %}
        {% endblock %}
      </div>
    </div>
  </body>
</html>

macros.html

{% macro show_job(job) %}
    <div class="card adjust-height">
      <header class="card-header">
        <p class="card-header-title">
          <a href="">{{ job['title'] }}</a>
        </p>
      </header>
      <div class="card-content">
        <div class="content">
          <p>
            <a href="">{{ job['employer_name'] }}</a><br>
              ${{ job['salary'] }}</br>
              {{ job['description'] }}
          </p>
        </div>
      </div>
    </div>
      {% endmacro %}

      {% macro show_jobs(jobs) % }
      <div class="column is-multiline">
        {% for job in jobs %}
        <div class="column is-half">
          {{ show_job(job) }}
        </div>
      {% endmacro %}

还有app.py:

import sqlite3
from flask import Flask, render_template, g

PATH = 'db/jobs.sqlite'

app = Flask(__name__)

def open_connection():
    connection = getattr(g, '_connection', None)
    if connection == None:
        connection = g._connection = sqlite3.connect(PATH)
    connection.row_factory = sqlite3.Row
    return connection

def execute_sql(sql, values=(), commit=False, single=False):
    connection = open_connection()
    cursor = connection.execute(sql,values)
    if commit == True:
        results = connection.commit()
    else:
        results = cursor.fetchone() if single else cursor.fetchall()

    cursor.close()
    return results

@app.teardown_appcontext
def close_connection(exeption):
    connection = getattr(g, '_connection', None)
    if connection is not None:
        connection.close()

@app.route('/')
@app.route('/jobs')
def jobs():
    jobs = execute_sql('SELECT job.id, job.title, job.description, job.salary, employer.id as employer.name as employer_name FROM job JOIN employer ON employer.id = job.employer_id')
    return render_template('index.html', jobs=jobs)

我试图找到错别字并尽可能地遵循教程,但是我似乎没有找到解决方法。

2 个答案:

答案 0 :(得分:0)

在您的macros.html中,第20行的%}之间没有空格

答案 1 :(得分:0)

像Tristo一样发现了您的错误,但我注意到您可以在代码中进行其他改进。

在Jinja2中使用宏时,如果不使用{%--%}分隔符,则可能会遇到空格问题。现在编写宏的方式是在宏输出中包括所有空格(制表符,空格,换行符)。

您可以找到更多信息here。除了对宏及其调用使用空格删除定界符之外,还可以考虑为项目启用lstrip_blockstrim_blocks