标签:err code temp delete post environ 传参 get 函数名
Django是一个大而全的框架,Flask是一个轻量级的框架。
区别:
Flask框架依赖werkzeug wsgi,flask本身没有的wsgi。
from werkzeug.serving import run_simple
class Flask(object):
def __call__(self,environ, start_response):
return "xx"
def run(self):
run_simple('127.0.0.1', 5000, self)
app = Flask()
if __name__ == '__main__':
app.run()
@app.route("/index")
def index():
data_dict = DATA_DIC
return render_template("index.html",data_dict=data_dict)
@app.route("/login",methods=["POST","GET"])
from flask import Flask,render_templaterequest,url_for
app = Flask(__name__)
@app.route("/index",endpoint="homepage") # 设置别名
def index():
data_dict = DATA_DIC
return render_template("index.html",data_dict=data_dict)
@app.route("/edit")
def edit():
return redirect(url_for("homepage")) # 使用别名
@app.route("/login",methods=["POST","GET"]) # 设置请求方式
def login():
user = request.form.get("user") # 通过request.form获取post数据
pwd = request.form.get("pwd")
if user == "xxx" and pwd == "123":
return redirect("/index/")
error = "用户名或密码错误"
return render_template("login.html",error=error)
@app.route("/edit",methods=["POST","GET"])
def edit():
nid = request.args.get("nid") # request.args获取get请求中的值
# 请求URL:http://127.0.0.1:5000/edit?nid=2
@app.route("/delete/<int:nid>") # 接收数据,设置数据类型
def delete(nid):
del DATA_DIC[nid]
return redirect(url_for("homepage"))
return "xxx" # 返回字符串
return jsonify({"code":1000,"msg":"xxxx"}) # 返回json格式数据
return render_template("login.html",xxx=a) # 返回模板,可以传参数
return redirect("/index/") 或 redirect(url_for("homepage")) # 跳转到URL,或别名
标签:err code temp delete post environ 传参 get 函数名
原文地址:https://www.cnblogs.com/os-linux/p/11907792.html