【问题标题】:Overwriting blocks in jinja在 jinja 中覆盖块
【发布时间】:2017-04-10 18:56:15
【问题描述】:

所以我正在学习 Jinja,并尝试使用扩展功能来用新内容覆盖块,但它不起作用。

home.html

      {% block mainBlock %}
   <div id="newNewsCont">
      <div id="leftCont">
        <h2> Tesla Model 3 </h2>
        <p> Model 3 combines real world range, performance, safety and spaciousness into a premium saloon that only Tesla can build. Our most affordable range yet, Model 3 acheives 215 miles of rage per charge while starting at only 35,000 USD before incentives. Model 3 is designed to attain the highest safety ratings in every category. </p>
      </div>

      <div id="rightCont">
        <h2> Reliant Robin LX 3DR 0.9 </h2>
        <p> 2 keys, cherry bomb exhaust, this car turns heads, just had brand new custom made interior fitted, with sound proofing under carpets, just had a full respray in renault 5 gt turbo pearl white </p>
      </div>
   </div>
  {%endblock%}

我想用 list.html 覆盖它

{% extends "home.html" %}

{% block mainBlock %}
    {% for item in cars %}
        <li> {{ item }}</li>
    {% endfor %}

{%endblock%}

server.py

import os
from flask import Flask, redirect, request, render_template

DATABASE = 'database.db'


app = Flask(__name__)

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

@app.route("/")
def home():
return render_template('list.html', msg = '')

@app.route("/ListCars")
def cars():
cars = ['tesla','reliant robin','Transit Van']
return render_template('list.html', cars = cars)

if __name__ == "__main__":
app.run(debug=True)

【问题讨论】:

  • “不工作”到底是什么意思?你可以看看 Jinja2 继承部分jinja.pocoo.org/docs/dev/templates/#template-inheritance
  • 哦,我已经阅读了很多次该页面,但它对我来说毫无意义。我希望 list.html 中 mainblock 的内容覆盖 home.html 中 mainblock 的内容
  • 加载list.html的结果是什么?

标签: html ajax flask jinja2


【解决方案1】:

如果您想为list.html 保留home.html 设计:

list.html

{% block mainBlock %}
{{super()}}
...
{%endblock%}

如果这不是你想要的,那么写出确切的问题,因为你的代码很好应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多