qiuyouzhi 发表于 2020-3-15 13:41:41

Flask学习笔记7:模板

本帖最后由 qiuyouzhi 于 2020-3-15 14:06 编辑

Flask学习笔记7:模板

我们之前写的代码,呈现出的网页都太简单,为了能显示更

复杂的网页,我们要使用模板.

代码:
#模板可以自己随便写一个,比如 <h1>Hello World!</h1>
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html') # 渲染模板

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name=name)

if __name__ == "__main__":
    app.run()

暗夜之隐 发表于 2020-4-2 19:20:25

index.html 这个文件应该放到这个py文件同目录对吗

qiuyouzhi 发表于 2020-4-2 19:22:11

暗夜之隐 发表于 2020-4-2 19:20
index.html 这个文件应该放到这个py文件同目录对吗

不是,目录差不多是这样的:
|主目录
-----main.py
-----templates
----------index.html

暗夜之隐 发表于 2020-4-2 19:22:45

from flask import Flask

from flask import render_template
import time
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html') # 渲染模板

@app.route('/time')
def t():
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime())
    return now

if __name__=="__main__":
    app.run(host="0.0.0.0", port=8090)

我这样访问不了http://127.0.0.1:8090/ 不知道什么问题    访问http://127.0.0.1:8090/time 是正常的

暗夜之隐 发表于 2020-4-2 19:26:29

qiuyouzhi 发表于 2020-4-2 19:22
不是,目录差不多是这样的:
|主目录
-----main.py


这样呀好的 谢谢了
页: [1]
查看完整版本: Flask学习笔记7:模板