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模块. ...
随机推荐
- ML01 机器学习后利用混淆矩阵Confusion matrix 进行结果分析
目标: 快速理解什么是混淆矩阵, 混淆矩阵是用来干嘛的. 首先理解什么是confusion matrix 看定义,在机器学习领域,混淆矩阵(confusion matrix),又称为可能性表格或是 ...
- 调用接口http封装
public static String httpHandler(String URL,String xml){ try { URL url=new URL(URL); URLConnection c ...
- 【npm】如何在Atom中安装emmet和atom-beautify插件?
为了提高编写HTML和CSS的速度,最近尝试着在Atom中安装emmet插件,下面谈谈安装成功的过程 1首先我尝试了网上教程中介绍最多的方法:打开Atom的引导界面(Welcome Guide)中的i ...
- nodejs实现OAuth2.0授权服务
OAuth是一种开发授权的网络标准,全拼为open authorization,即开放式授权,最新的协议版本是2.0. 举个栗子: 有一个"云冲印"的网站,可以将用户储存在Goog ...
- 《On Writing Well 30th Anniversa》【PDF】下载
<On Writing Well 30th Anniversa>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382210 内容简 ...
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- iOS Swift基础知识代码
推荐:Swift学习使用知识代码软件 //集合类型 数组 字典 func array1(){ var arr = [","dd"] //简单写法 var arr1 = [ ...
- iOS控制器跳转动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 MyViewController *myVC = [[MyViewController alloc]init]; //创建动画 C ...
- iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转
只含有 .关于横竖屏的代码 #import "InspectionReportViewController.h" #define SCREEN_WIDTH ([UIScreen m ...
- C#基础在using中创建对象
在using中创建的对象的类必须是实现了IDispose接口的类,示例代码如下: static void Main(string[] args) { Method(); Console.WriteLi ...