首页 > 解决方案 > 当文件从另一个文件扩展时,不显示 .html 文件的内容

问题描述

我有一个index.html,我想将其内容扩展到其他页面。如果我将index.html扩展到landing-page.html,则会显示index.html中的内容,但不会显示landing-page.html中的内容。

路线.py

@app.route('/welcome/')
def landing_page():
    return render_template('landing-page.html')

登陆页面.html

{% extends "index.html" %}

{% block content %}

  <p>This is from the landing-page</p>

{% endblock content %}

索引.html

<!DOCTYPE html>
<html lang="en">
  <head>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

      <!-- Bootstrap CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

      {% if title %}
        <title>Test Title - {{ title }}</title>
      {% else %}
        <title>Test Title</title>
      {% endif %}
  </head>
  <body>

    <p>This is from the index page</p>

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>
  </html>

使用以下路径访问landing-page.html 时,/welcome/输出如下: 在此处输入图像描述

从上图中可以看出,landing-page.html的内容并没有显示出来。我无法弄清楚为什么。

标签: htmlflaskjinja2

解决方案


/welcome 路由运行正常,但两者均未连接。
需要2个额外的东西:

  1. 扩展功能(并建立连接)
def landing_page():  
    return render_template('landing-page.html',  
    my_string='FirstInclude')
  1. 将包含在 index.html 某处(并因此放置连接)

    <p>{{我的字符串}}</p>


推荐阅读