template讲解

flask支持将所有的页面,统一放到template中管理,主APP文件中只是引用

#步骤1,在template中写页面

#步骤2,导入template

#步骤3,在视图函数中,response中,使用   render_template('posts/list.html')

示例代码:

from flask import Flask,render_template

app = Flask(__name__)


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

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

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

---------------------------

如果不想把模板文件放到template中,还可以在APP初始化的时候,指定模板的位置

app = Flask(__name__,template_folder='C:/templates')
 

猜你喜欢

转载自blog.csdn.net/hebi123s/article/details/81778625