模版的继承

在templatest文件夹下

新建 base.html (待被继承模版)

新建 test.html  (继承base.html的文件)

 

base.html内容如下:

<!DOCTYPE html>

<html lang="en">

<hrad>

  <meta charset="utf-8">

  <title>Title</title>

</hrad>

<body>

  {% block  head %}

      <div>This is head</div>

  {% endblock %}

 

  {% block  content %}

      <div>This is content</div>

  {% endblock %}

  

  {% block  foot %}

      <div>This is foot</div>

  {% endblock %}

</body>

</html>

 

test.html 文件内容如下:

{% extends "base.html" %}

{% block  content %}

  {{ super() }}      #加上super()关键字就会把 base.html 文件原来  block  content里面内容也带上

  test文件自己的内容,替换掉base.html文件中 block  content的区域 信息展示

{% endblock %}

 

视图函数调用

@web.route("/test")
def test():
  #模版
  return render_template('test.html')

 

浏览器输出:

This is head

This is content

test文件自己的内容,替换掉base.html文件中 block  content的区域 信息展示

This is foot

posted @ 2020-05-11 22:08  凯宾斯基  阅读(177)  评论(0编辑  收藏  举报