get请求:先导入request的包,request的参数
args(获取get方法传递过来的变量)、base_url、blueprint、charset、cookies、form(获取post传递过来的变量)、get_data、get_json、headers、host、host_url、json、method、path、pragma、url(当前的url)、url_charset、url_root、url_rule、user_agent、values、view_args
from flask import Flask,render_template,request
app=Flask(__name__,template_folder='view')
@app.route('/index')
def index():
name=request.args.get('name')
print(name)
return render_template('request.html')
if __name__ == '__main__':
app.run(debug=True)
先一个登录和注册功能(post请求)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form method="post" action="/login">
<input type='text' name="username" placeholder="username">
<input type='password' name='password' placeholder="password">
<input type="submit">
</form>
</body>
</html>
from flask import Flask,render_template,request
app=Flask(__name__,template_folder='view')
@app.route('/index')
def index():
return render_template('request.html')
@app.route('/login',methods=['POST','GET'])
def login():
if request.method == 'GET':
return render_template('login.html')
elif request.method == 'POST':
name = request.form.get("username")
password = request.form.get('password')
print(name)
print(password)
if name == 'admin' and password == '123':
return 'success'
else:
return 'fail'
if __name__ == '__main__':
app.run(debug=True)
flask的响应:导入包make_response
from flask import Flask,render_template,request,make_response
app=Flask(__name__,template_folder='view')
@app.route('/response')
def response():
# response=make_response(render_template('response.html'))
response = make_response('hello word')
response.headers['name']='admin'
response.set_cookie('hello')
return response
if __name__ == '__main__':
app.run(debug=True)