http请求中产生的两个核心对象

http请求: HttpRequest http响应: HttpResponse 所在位置 django.http

httpRequest属性:

  HttpRequest.path

    请求页面的全路径,不包含域名---完整的请求路径,不包括域 例: "/music/bands/the_beatles/"。


-  HttpRequest.method
请求中使用的HTTP方法的字符串表示。全大写表示。例如 if request.method == 'GET':
do_something()
elif request.method == 'POST':
do_something_else()

HttpRequest.GET

    GET请求对象,包含HTTP GET参数的类字典对象,详细查看 QueryDict文档

HttpRequest.POST

    POST请求对象,包含HTTP GET参数的类字典对象,详细查看 QueryDict文档,

    服务器收到空的post请求的情况也是可能发生的,也就是说,表单from通过http post
方法提交请求,单是表单中可以没有数据。因此,不能使用御酒 if request.POST来判断
是否使用HTTP POST方法,应该使用if request.method == "POST" (参见本表的method
属性)。
注意: POST不包含file-upload(文件上传)信息,请看 FILES

HttpRequest.REQUEST  django 1.8之后被废弃了

    为了方便,该属性是POST和GET属性的集合体,但是有特殊性,先
找到POST属性,然后在查找GET属性。借鉴PHP`s$_REQUEST。 例如,如果GET={"name":"john"}和POST={"age":'34'},册 REQUEST['name']
的值为"jhon",REQUEST["age"]的值是'34'. 强烈建议使用GET and POST,因为这两个属性更加显示化,写出来的代码更易于
理解

HttpRequest.COOKIES

    包含所有cookies的标准 python字典对象,keys 和values都是字符串 参见12章 有关于
cookies更详细的讲解

-  HttpRequest.FILES

    包含所有上传文件的类字典对象。FILES中每个KEY都是<input type="file" name=""/>
标签中的name属性的值,FILES中每个value同事也是一个标准的python字典对象,包
含下面三个Keys:
1. filename 上传文件名,用python字符串表示
2. content-type 上传文件的Content type
3. content 上传文件的原始内容
注意: 只有在请求方法是POST,并且请求页面中<from>有enctype="multipart/frm-data"
属性时FILES才拥有数据。否则FILES是一个空字典

- HttpRequest.META

    包含所有可用HTTP头部信息的字典。例如:
1. CONTENT_LENGTH
2. CONTENT_TYPE
3. QUERY_STRING:未解析的原始查询字符串
4. REMOTE_ADDR:客户端主机名
5. SERVER_NAME:服务器主机名
6. SERVER_PORT :服务器端口
META中写着头加上前缀HTTP_ KEY 例如:
1. HTTP_ACCEPT_ENCODING
2. HTTP_ACCEPT_LANGUAGE
3. HTTP_POST 客户发送的HTTP主机头信息
4. HTTP_REFERER:referring页面
5. HTTP_USER_AGENT:客户端的user-agent字符串
6. HTTP_X_BENDER:X-Bender头部信息

- HttpRequest.user

    是from django.contrib.auth.models.User对象,代表当前登录的用户,如果访问用户当前没有
登录,user将被初始化为from django.contrib.auth.models.AnonymousUser的实例。
你可以通过user的is_authenticated()方法来辨别用户是否登录
if request.user.is_authenticated():
do something for logged-in users
else:
do something for anonymous users
只有激活django中的 A uthenticationMiddleware时该属性才可用

- HttpRequest.session

    唯一可读写的属性,代表当前回话的字典对象,只有激活django中的
session支持是该属性才可用

-raw_post_data

    原始HTTP POST数据,未解析过。改机处理时会有用。

- HtppRequest对象的方法部分
1. get_full_path()
返回包含查询字符串的请求路径。例如,
"/music/bands/the_beatles/?print=true"。
2. QueryDict对象
get() 如果key对应多个value,get()返回最后一个value。
在httprequest对象中,GET和POST属性时 django.http.QueryDict类的实例。

HttpResponse

对于Httprequest对象来说 django自动创建,但是 httpresponse对象必须 我们自己创建 每个View方法必须返回一个HTTPResponse对象。 HTTPResponse类在django.http.HttpResponse.render方法包装的也是一个response对象。

构造HtppResponse:

response = HttpResponse("Here's the text of the Web page.")

response = HttpResponse("Text only,please.",mimetype="text/plain")

HtppResponse的子类 部分

  • HttpResponseRedirect

    构造函数接受单个参数:重定向到URL。可以是全URL(e.g.,'http://search.yahoo.com/') 或者相对URL(e.g.,'/seach/').注意折返回HTTP状态码302

  • HttpResponsePermanentRedirect

    同HttpResponseRedirect一样,但它返回永久重定向(HTTP状态代码301)而不是“找到”重定 向(状态代码302)。

  • HttpResponseNotModified

    构造函数不接受任何参数,也不会在此响应中添加任何内容。用于指定自用户最后一次请求 (状态代码304)以来页面尚未被修改。

  • HttpResponseBadRequest

    HttpResponse使用的就是使用400状态码。

  • HttpResponseNotFound

    返回404状态码

  • JsonResponse

    返回Json字符串

详见: https://docs.djangoproject.com/en/1.11/ref/request-response/

  • httpresponse对象上扩展的常用方法

render rander_to_response redirect 跳转的方法 更多详见: https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/

三. 常用的其他方法

locals() 可以直接将函数中所有变量全部传递给模板

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

Django-Views模块详解的更多相关文章

  1. 第五篇Django URL name 详解

    Django URL name 详解 利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护. Djan ...

  2. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  3. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  4. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  5. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  6. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

  7. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  8. 小白的Python之路 day5 time,datatime模块详解

    一.模块的分类 可以分成三大类: 1.标准库 2.开源模块 3.自定义模块 二.标准库模块详解 1.time与datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时 ...

  9. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

  10. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

随机推荐

  1. 为 NativeScript 项目添加 iOS / Android 平台 API 的智能感知

    使用 NativeScript ,我们可以很容易的调用平台的原生 API,在开发过程中,我们可以添加这些 API 的 d.ts 文件来提供智能感知,帮助我们更方便的构建媲美原生的 APP. 首先通过 ...

  2. 在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序

    引子 在此前的一篇文章中,我介绍了如何在本地docker环境中运行ASP.NET Core跨平台应用程序(http://www.cnblogs.com/chenxizhang/p/7148657.ht ...

  3. 一位10年Java工作经验的架构师聊Java和工作经验

    从事近十年的 JavaEE 应用开发工作,现任阿里巴巴公司系统架构师.对分布式服务架构与大数据技术有深入研究,具有丰富的 B/S 架构开发经验与项目实战经验,擅长敏捷开发模式.国内开源软件推动者之一, ...

  4. 翻译:MariaDB DATABASE()

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  5. WEB版一次选择多个图片进行批量上传(WebUploader)的解决方案

    最近在学习百度的开源上传组件WebUploader,上一篇文章,学习了批量文件上传,今天学习一下批量图片上传,实际上与文件上传很类似,只是添加了图片显示功能,这个功能WebUploader组件中已经提 ...

  6. spring boot https --restful接口篇

    我们写的接口默认都是http形式的,不过我们的接口很容易被人抓包,而且一抓全是明文的挺尴尬的 spring boot配置https生成证书大的方向有3种: 1.利用keytool自己生成证书 2.从免 ...

  7. 变量声明declare,简单运算符运算,变量测试与内容替换

    declare -/+ 选项 变量名 - 设类型 + 取消类型 -i 设为整型 -x 设为环境变量 -p 显示类型属性(property) [root@localhost ~]# a= [root@l ...

  8. Android破解学习之路(四)——Android游戏 3D摩托飞车破解

    经过前面三期的破解,想必大家已经非常熟悉破解的流程,这一篇也算是练手项目,我们继续来练习吧 apk下载地址:链接: https://pan.baidu.com/s/1sl3b3R3 密码: 6666 ...

  9. iOS常见的几种加密方法(base64.MD5.Token传值.系统指纹验证。。加密)

    普通加密方法是讲密码进行加密后保存到用户偏好设置中 钥匙串是以明文形式保存,但是不知道存放的具体位置 一. base64加密 base64 编码是现代密码学的基础 基本原理: 原本是 8个bit 一组 ...

  10. go defer (go延迟函数)

    go defer (go延迟函数) Go语言的defer算是一个语言的新特性,至少对比当今主流编程语言如此.根据GO LANGUAGE SPEC的说法: A "defer" sta ...