flask学习之我的第一个flask页面<一>

首先装好python  pycharm,然后再装上flask ,pip install flask即可

先新建一个项目:flaskone


在flaskone下新建python文件:

from flask import Flask,render_template

app = Flask(__name__)  #使用当前模块

@app.route('/hello/<name>')   
def hello_world(name):  #参数名与url的参数名一致,都必须是name
   return render_template('hello.html',names=name)  #前一个参数必须与模板页面中的参数名一致为names

if __name__ == '__main__':
   app.run(debug=True)    #开启调试模式,run方法用来启动服务

在flaskone下新建templates文件夹,在下面新建hello.html网页   (templates文件夹与python文件应该在同一层级才可以)

<!doctype html>
<html>
   <body>

      <h1>大家好 我叫 {{ names }}!</h1>

   </body>
</html>

启动该脚本之后,

浏览器输入http://127.0.0.1:5000/hello/emily,结果展示如下

原文地址:https://www.cnblogs.com/lelexiong/p/13410963.html