Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用从一个基础模板继承的多个模板?蟒蛇,烧瓶_Python_Html_Templates_Flask_Web Deployment - Fatal编程技术网

Python 如何使用从一个基础模板继承的多个模板?蟒蛇,烧瓶

Python 如何使用从一个基础模板继承的多个模板?蟒蛇,烧瓶,python,html,templates,flask,web-deployment,Python,Html,Templates,Flask,Web Deployment,我的目录结构如下(来源): 我的hamlet.py应用程序使用以下函数初始化2页: from flask import render_template from app import app @app.route('/') @app.route('/index') @app.route('/instance') def index(): return render_template('index.html') def instance(): return render_te

我的目录结构如下(来源):

我的
hamlet.py
应用程序使用以下函数初始化2页:

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
@app.route('/instance')

def index():
    return render_template('index.html')

def instance():
    return render_template('instance.html')
instance.html
index.html
都是从具有不同块内容的
base.html
继承的,base.html如下所示:

<!DOCTYPE html>
<html lang="en">

    <head>
        <title>Post Editor Z</title>
    </head>

    <body>

        <div class="container">
            {% block content %}{% endblock %}
        </div><!-- /.container -->

    </body>
</html>
如何解决instance.html和index.html将显示模板中定义的两个不同页面的问题?


是因为我在
hamlet.py
中错误地初始化了我的页面吗?

我找到了解决问题的方法,但我不知道为什么它会起作用

@app.route('/instance')
移动到
instance()
工作之前:

from flask import render_template
from app import app

@app.route('/')

@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/instance')
def instance():
    return render_template('instance.html')

它之所以有效,是因为
@app.route
是一个函数的装饰器。像以前一样编写,每个url都指向索引函数。这样你就有了
/instance
/instance()
和其他
索引()
。如果
@app.route('/')
没有装饰任何东西,下一个函数是
@app.route('index')
。默认页面将指向
索引()
@app.route('/')
在本例中与
@app.route('/index')
一起修饰
索引。现在您的默认页面是
index()
{% extends "base.html" %}
{% block content %}    
 <div class="row">
    <div class="col-lg-12">
      Hello World
    </div>
 </div>
{% endblock %}
{% extends "base.html" %}
{% block content %}    
 <div class="row">
    <div class="col-lg-12">
      Some instance.
    </div>
 </div>
{% endblock %}
{% extends "abase.html" %}
{% block content %}    
 <div class="row">
    <div class="col-lg-12">
      Hello World
    </div>
 </div>
{% endblock %}
from flask import render_template
from app import app

@app.route('/')

@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/instance')
def instance():
    return render_template('instance.html')