Django-Views模块详解
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模块详解的更多相关文章
- 第五篇Django URL name 详解
Django URL name 详解 利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护. Djan ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- python time 模块详解
Python中time模块详解 发表于2011年5月5日 12:58 a.m. 位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...
- python time模块详解
python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明 一.简介 ...
- 小白的Python之路 day5 time,datatime模块详解
一.模块的分类 可以分成三大类: 1.标准库 2.开源模块 3.自定义模块 二.标准库模块详解 1.time与datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时 ...
- 小白的Python之路 day5 random模块和string模块详解
random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...
- Python中time模块详解
Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...
随机推荐
- mysql默认安装目录说明
MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的初学者,因为 Linux本身的目录结构就比 ...
- C# 委托与事件详解(二)
什么是事件?EVENT?点击事件?加载事件?一连串的模糊的概念冲击着我们弱小的脑袋 那我们首先来看一下比较正统的感念吧: 事件是类在发生其关注的事情时用来提供通知的一种方式. 事件的发生一般都牵扯2个 ...
- JavaScript 中 apply 、call 的详解
apply 和 call 的区别 ECMAScript 规范给所有函数都定义了 call 与 apply 两个方法,它们的应用非常广泛,它们的作用也是一模一样,只是传参的形式有区别而已. 原文作者:林 ...
- iOS Block的简单使用以及__block 和static修饰变量
简单的代码总结,不足之处多多指教. //简单的使用 -(void)blockOne{ ; int(^BlockOne)(int) = ^(int num2) { return number*num2; ...
- CSS 水平居中/布局 垂直居中 (月经问题)
水平居中 如果它是一个行内元素 对其父元素使用 text-align:center 即可实现. <p style = " text-align:center; width:300px; ...
- 如何检测mvc性能和sql语句
mvc中使用linq如何检测sql语句 .net中使用mvc开发已经是一种趋势,不仅仅是.net ,java 等越来越多的开发者更倾向于mvc这种开发模式,在.net mvc 使用linq非常方便,各 ...
- SpringMVC底层数据传输校验的方案(修改版)
团队的项目正常运行了很久,但近期偶尔会出现BUG.目前观察到的有两种场景:一是大批量提交业务请求,二是生成批量导出文件.出错后,再执行一次就又正常了. 经过跟踪日志,发现是在Server之间进行jso ...
- pwd 命令详解
pwd 作用: 以绝对路径的方式显示用户当前工作目录,命令将当前目录的全路径名称(从根目录)写入标准输出, 全部目录使用/分隔,第一个/表示根目录, 最后一个/ 表示当前目录. 执行pwd 命令可以立 ...
- Cleaner, more elegant, and harder to recognize(翻译)
Cleaner, more elegant, and harder to recognize 更整洁,更优雅,但更难识别 看来,有些人把我几个月前一篇文章的标题"Cleaner,more e ...
- PHP生成验证码
<?php check_code(); function check_code($width = 100, $height = 50, $num = 4, $type = 'jpeg') { $ ...