Flask框架:视图
Posted bad-robot
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask框架:视图相关的知识,希望对你有一定的参考价值。
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:",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([])
以上是关于Flask框架:视图的主要内容,如果未能解决你的问题,请参考以下文章