请你写出使用python flask渲染多个HTML的代码,其中有一个HTML具有汇总其余HTML的效果
时间: 2024-04-29 15:24:55 浏览: 184
Sure, here is an example code for using Python Flask to render multiple HTML templates, where one template acts as a summary of the others:
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/page1')
def page1():
return render_template('page1.html')
@app.route('/page2')
def page2():
return render_template('page2.html')
@app.route('/summary')
def summary():
content = {
'page1': render_template('page1.html'),
'page2': render_template('page2.html')
}
return render_template('summary.html', **content)
if __name__ == '__main__':
app.run()
```
In this code, we use Flask's `render_template` function to render HTML templates for each of our pages. The `index` function returns our main index.html file, while the `page1` and `page2` functions return their respective HTML pages.
The `summary` function uses the `render_template` function to render the page1.html and page2.html templates, and passes them as a dictionary to the summary.html template. The double-asterisk syntax (`**content`) in the `render_template` function unpacks the dictionary and passes its contents as arguments to the template.
On the summary.html template, we can then use the variables `page1` and `page2` to display the summary of our other HTML pages.
阅读全文