from flask import Flask,url_for,request
app = Flask(__name__)
@app.route("/")
def index():
#
# /article/2/ 只传一个aid
# 根据函数的名字进行反转 得到函数对应的路由 重定向
# /article/2/?page=2 aid=2,page=2 把不存在的数page=2当参数了
# /article/2/?page=2&t=123 传多个不存在参数 后面用&连接
print(url_for("article_list",aid=2,page=2,t=123))
return "hello world"
# http://127.0.0.1:5000/article/2/ 如何通过函数名得到url地址?
@app.route("/article/<aid>/")
def article_list(aid):
return "article list {}".format(aid)
@app.route("/detail/<did>/")
def article_detail(did):
# print(url_for("index"))
# /?next=%2F
print(url_for("index",next="/"))
# / => %2F
return "article detail {}".format(did)
# 默认都是接收GET请求 想要接收POST请求 需要怎么办?定义methods
@app.route("/login/",methods=["POST","GET"])
def login():
# get 参数直接在url中 一般都是获取数据
# post 参数没有直接体现在URL地址中 一般都是提交表单数据
# print(type(request.args))
# print(request.args.get('username'))
# 接收POST请求发送的参数如何接收
print(request.form.get("name"))
return "login"
if __name__ == '__main__':
app.run(debug=True)
下载:
接收POST请求发送的参数
Postman-添加name - send发送
打开Pycharm 接收到数据