一.安装
1. pip install flask
2. 通过pycharm ->setting->project->project interpreter 搜索Flask 进行安装
二.入门案例
#coding:utf-8
from flask import Flask #Flask 框架包、库、模块
app = Flask(__name__) #实例化Flask,赋值到app变量
@app.route(\"/\") #路由装饰器:识别访问路径
def hello_world(): #将响应 / 路径的请求动作,执行函数中的代码
return \"hello flask\" #返回响应的结果
if __name__ == \'__main__\':
app.run() #运行项目
三.路由及参数
@app.route(\"/postp/<int:stuid>/<string:stuname>\") #路由参数格式
def postparam(stuid,stuname): #路由响应函数要把参数作为形式参数
# print(\"接收参数\"+stuname)
# return \"接收参数:%d , %s\" %(stuid,stuname)
return \"接收参数:\"+str(stuid)+\",\"+stuname
四.视图模板的使用
from flask import render_template
...
#请求渲染模板:render_template 模块
@app.route(\"/\")
def index():
return render_template(\'/home/index.html\')
# 找templates(没有则创建)目录下的模板文件;
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。


