python-flask笔记(二·)

2018-07-26  本文已影响17人  f1a94e9a1ea7

Flask主要有两个依赖:

路由:

app = Flask(__name__)


@app.route('/')
def index():
    return 'Hello World!'
上面函数返回的一个字符串,也可以返回 html 页面:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Just for fun</title>
</head>
<body>

<h1>Hello</h1>

</body>
</html>
做到这里本应该返回hello的页面返回了hello world... 然后发现渲染到浏览器的根本就不是html页面,一直都是最开始写的helloworld字符串... 也就是说这个5000端口服务器运行的文件一直都是最开始的那个blog.py,后面再怎么修改重启,服务器上面运行的文件也没有更改掉, 暂停 找找原因
回来了
把5000端口换成别的就可以了....
app.run(port = 7777,debug=True)
接下来向html页面传入动态数据:
<h1>Hello {{username}}</h1>
@app.route('/<username>')
def hello_world(username):
    return render_template('index.html', username=username)
上一篇 下一篇

猜你喜欢

热点阅读