接触Django这么久了,从来没有好好学习关于Django中的Request和Response对象。借着文件上传下载的相关工作,现在总结一下也不错。
当一个页面请求过来,Django会自动创建一个Request对象,匹配URLconf中对应的view方法,并将这个Request对象作为第一个参数传递给view方法。而view方法会经过一系列操作之后,返回一个Response对象,返回给客户端。
具体的Request对象的属性(attribute)有很多(除了seesion都说只读属性):

  1. HttpRequest.path—不包括域的全路径,例如:”/music/bands/the_beatles/”
  2. HttpRequest.method—请求方法,常用的有GET和POST
  3. HttpRequest.encoding—请求的编码格式,很有用!
  4. HttpRequest.GET(POST)---见HttpRequest.method
  5. HttpRequest.REQUEST---类字典的对象,搜索顺序先POST再GET
  6. HttpRequest.COOKIES---标准的python字典对象,键和值都是字符串。
  7. HttpRequest.FILES---类字典对象。键是表单提交的name---<input type="file" name="" />而file是一个上传的对象,它的属性有:read,name,size,chunks
  8. HttpRequest.META---包括标准HTTP头的python字典。如下:

    • CONTENT_LENGTH
    • CONTENT_TYPE
    • HTTP_ACCEPT_ENCODING
    • HTTP_ACCEPT_LANGUAGE
    • HTTP_HOST — The HTTP Host header sent by the client.
    • HTTP_REFERER — The referring page, if any.
    • HTTP_USER_AGENT — The client’s user-agent string.
    • QUERY_STRING — The query string, as a single (unparsed) string.
    • REMOTE_ADDR — The IP address of the client.
    • REMOTE_HOST — The hostname of the client.
    • REMOTE_USER — The user authenticated by the web server, if any.
    • REQUEST_METHOD — A string such as "GET" or "POST".
    • SERVER_NAME — The hostname of the server.
    • SERVER_PORT — The port of the server.
  9. HttpRequest.user---当前登录的用户
  10. HttpRequest.session---一个可读写的类python字典
  11. HttpRequest.raw_post_data---在高级应用中应用,可以算是POST的一个替代,但是不建议使用。
  12. HttpRequest.urlconf---默认情况下,是没有定义的

每个Request对象还有一些很有用的方法:

  1. HttpRequest.get_host()—返回域名,例如:”127.0.0.1:8000″
  2. HttpRequest.get_full_path()—返回请求的全路径(但是不包括域名),例如:”/music/bands/the_beatles/?print=true”
  3. HttpRequest.build_absolute_uri(location)—以上2者的结合
  4. HttpRequest.is_secure()—判断是否为https连接(没有用过)
  5. HttpRequest.is_ajax()—请求为XMLHttpRequest时,返回True

下面看看HttpResponse的属性(attribute):

  1. HttpResponse.content—python string对象,尽量用unicode。
  2. HttpResponse.status_codeHTTP Status code

HttpResponse的方法:

  1. HttpResponse.has_header(header)
  2. HttpResponse.set_cookie
  3. HttpResponse.delete_cookie
  4. HttpResponse.write(content)
  5. HttpResponse.flush()
  6. HttpResponse.tell()

以上说明以后陆续修正吧。
下面一部分是我学习到的东西:告诉浏览器你要下载文件(TELLING THE BROWSER TO TREAT THE RESPONSE AS A FILE ATTACHMENT)!

>>> response = HttpResponse(my_data, mimetype='application/vnd.ms-excel')
>>> response['Content-Disposition'] = 'attachment; filename=foo.xls'
FROM:http://py-bow.appspot.com/?p=20001

最新文章

  1. GO语言学习
  2. 《C++必知必会》学习笔记
  3. 找不到 com.google.zxing.ResultMetadataType 异常解决
  4. poj3378
  5. 抽象类 接口 虚函数(C++模拟,个人见解)
  6. Codeforces 112B-Petya and Square(实现)
  7. Centos清理内存 内存回收释放及内存使用查看的相关命令
  8. 新的 Windows Azure 网络安全性白皮书
  9. [编织消息框架][netty源码分析]14 PoolChunk 的 PoolSubpage
  10. FORM界面批量处理-全选框实现
  11. web---资源的下载及中文乱码问题
  12. ZOJ4043 : Virtual Singers
  13. xxxx-xx-xx的时间的加减
  14. 一文搞定scrapy爬取众多知名技术博客文章保存到本地数据库,包含:cnblog、csdn、51cto、itpub、jobbole、oschina等
  15. find算法
  16. vue 之 Virtual Dom
  17. Miller_Rabbin算法判断大素数,Pollard_rho算法进行质因素分解
  18. [转] What is a Full Stack developer?
  19. sql server 查询本周、本月所有天数的数据
  20. ios 重用UI部分代码的好方法(再也不用为局部变量的命名而烦恼啦!)

热门文章

  1. python字符串,列表常用操作
  2. vue -webpack.dev.config.js模拟后台数据接口
  3. 通过printf从目标板到调试器的输出
  4. Confluence 6 管理协同编辑 - 审计的考虑
  5. Confluence 6 SQL Server 数据库驱动修改
  6. Confluence 6 配置默认语言界面
  7. Burp Scanner Report
  8. CSS----布局不理解
  9. 高斯消元-poj1222熄灯问题状态压缩解法
  10. php url函数