【问题标题】:How to resolve a jinja2.exceptions.TemplateSyntaxError?如何解决 jinja2.exceptions.TemplateSyntaxError?
【发布时间】:2017-06-04 07:31:49
【问题描述】:

我有如下 HTML 代码:

<html>
<head>
	<title>Booking Lah</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <style type="text/css">
    .card {
      height: 450px;
    }
    .card-image {
      max-height: 300px;
      max-width: 350px;
      width: auto;
      vertical-align: middle;
    }
    .image-display {
      height: 300px;
      width: auto;
    }
    .helper {
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
    .image-caption {
      height: 100px;
    }
    .star {
      color: orange;
    }
  </style>
</head>

<body>
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="home.php">Booking Lah</a>
    </div>
    <ul class="nav navbar-nav navbar-right">
      <li><a href="register.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
      <li><a href="login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
    </ul>
  </div>
</nav>

<div class="container">
<div class="row">

{{% for row in rows %}}
<div class="col-sm-4 card">
  <div class="image-display">
      <center>
          <span class="helper"></span><img src="{{row['imgurl']}}" class="card-image"><br>
      </center>
  </div>
      <div class="image-caption">
        <center>
          <h1><a href="#">{{row["name"]}}</a></h1>
          <h2>
            <span class="glyphicon glyphicon-star star"></span>
          </h2>
        </center>
      </div>
    </div>
{{% endfor %}}

</div>
</div>

</body>
</html>

我收到这样的错误:

jinja2.exceptions.TemplateSyntaxError TemplateSyntaxError: 意外的 '%' 文件“/home/m26415086/UHTOS/templates/home.html”,第 55 行,在模板中 {{% for row in rows %}}

我已经尽力了,但没有任何改变。

【问题讨论】:

    标签: python sqlite flask


    【解决方案1】:

    在您的模板中,您使用的是{{%%}},而不是{%%}

    {% 用于指令(循环、条件等),{{ 用于替换。这里是doc 供进一步参考。

    【讨论】:

    • 谢谢你,我没有注意到,但现在我有另一个问题,循环不工作它不会显示我放入循环中的任何元素
    • tmplt = Template(...) 然后tmplt.render(rows=[{'name': 'first row', 'imgurl': 'first imgurl'}, {'name': 'second row', 'imgurl': 'second imgurl'}]) 适合我...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2019-11-07
    • 2020-11-21
    • 2020-08-10
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多