Flask框架:檢視
阿新 • • 發佈:2018-12-07
1、檢視中請求request的屬性
request.method request.args request.form request.values request.cookies request.headers request.path request.full_path request.script_root request.url request.base_url request.url_root request.host_url request.host request.files obj = request.files['the_file_name'] obj.save('/var/www/uploads/' + secure_filename(f.filename))
測試程式碼:
1 from flask import Flask,request,Response 2 3 app = Flask(import_name=__name__) 4 5 @app.route('/index/') 6 def index(): 7 print("request.method:",request.method) 8 print("request.args:",request.args) 9 print("request.form:View Code",request.form) 10 print("request.values:",request.values) 11 print("request.cookies:",request.cookies) 12 print("request.headers:",request.headers) 13 print("request.path:",request.path) 14 print("request.full_path:",request.full_path) 15 print("request.script_root:",request.script_root)16 print("request.url:",request.url) 17 print("request.base_url:",request.base_url) 18 print("request.url_root:",request.url_root) 19 print("request.host_url:",request.host_url) 20 print("request.files:",request.files) 21 return Response("index") 22 23 if __name__ == '__main__': 24 app.run()
使用瀏覽器訪問:http://127.0.0.1:5000/index/?a=1&b=2
執行結果:
request.method: GET request.args: ImmutableMultiDict([('a', '1'), ('b', '2')]) request.form: ImmutableMultiDict([]) request.values: CombinedMultiDict([ImmutableMultiDict([('a', '1'), ('b', '2')]), ImmutableMultiDict([])]) request.cookies: {'sessionid': 'o3fu279ez1lnsuoung8udxc95sxbglde', 'csrftoken': 'ISStt8sv8ATqEPt0MWfHiy6sqNSmz0GYB35Yam3SyAGcYBRzxnx4rsbr1XeNDF3I'} request.headers: Host: 127.0.0.1:5000 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 Cookie: sessionid=o3fu279ez1lnsuoung8udxc95sxbglde; csrftoken=ISStt8sv8ATqEPt0MWfHiy6sqNSmz0GYB35Yam3SyAGcYBRzxnx4rsbr1XeNDF3I request.path: /index/ request.full_path: /index/?a=1&b=2 request.script_root: request.url: http://127.0.0.1:5000/index/?a=1&b=2 request.base_url: http://127.0.0.1:5000/index/ request.url_root: http://127.0.0.1:5000/ request.host_url: http://127.0.0.1:5000/ request.files: ImmutableMultiDict([])