一.HttpRequest对象

HttpRequest在django.http这个模块中
它是用django创建
文档https://docs.djangoproject.com/en/1.11/ref/request-response/#httprequest-objects
属性:
HttpRequest.body
HttpRequest.path 不带参数的请求路径
HttpRequest.method
HttpRequest.encoding
HttpRequest.GET
HttpRequest.POST
HttpRequest.META
HttpRequest对象——方法:
HttpRequest.get_host() 获取主机名
HttpRequest.get_port()
HttpRequest.get_full_path() 获取完整请求路径
HttpRequest.is_secure() 是否是安全的
HttpRequest.is_ajax() 是否是ajax

属性查看效果:

views.py:

from django.http import HttpResponse
def index(request):
print(request.scheme)
print(request.method)
print(request.path)
print(request.body)
print(request.encoding)
print(request.POST)
print(request.META)
print(request.GET)
return HttpResponse("Hello world !!!")

刷新浏览器后终端效果如下:

方法查看效果:

def index(request):
print(request.get_host())
print(request.get_port())
print(request.get_full_path())
print(request.build_absolute_uri())
print(request.is_secure())
print(request.is_ajax())
print(request.readline())
print(request.__iter__())
return HttpResponse("Hello world !!!")

刷新浏览器后终端效果如下:

 二.HttpResponse对象

HttpResponse在django.http这个模块中
传递一个字符串作为页面的内容到 HttpResponse 构造函数中即可
>>> from django.http import HttpResponse 导入模块
>>> response = HttpResponse("Here's the text of the Web page.") 实例化模块
>>> response = HttpResponse("Text only, please.", content_type="text/plain")
https://docs.djangoproject.com/en/1.11/ref/requestresponse/#httpresponse-objects HttpResponse对象——属性:
属性:
HttpResponse.content 返回普通的字符串
HttpResponse.charset 返回的字符集
HttpResponse.status_code 返回的状态码
HttpResponse.reason_phrase
方法:
HttpResponse.__init__(content=”, content_type=None, status=200, reason=None, charset=None)

(1)views.py: httpresponse返回字符串

def index(request):
res = HttpResponse()
res.content = "你好"
res.status_code = 300
return res

刷新浏览器后效果如下:

(2)views.py: httpresponse返回数据类型要特殊处理,因为它默认接收的是字符串

from django.http import HttpResponse
import json
def index(request):
data = ["a","b","c"] #列表
return HttpResponse(json.dumps(data)) #返回列表

其实上述效果可直接用JsonResponse对象解决,不用再用json.dumps序列化

三.JsonResponse对象

from django.http import HttpResponse,JsonResponse
def index(request):
data = ["a","b","c"] #列表
return JsonResponse(data,safe=False) #data是列表则safe参数要设置false,是字典则true,默认就是true所以不用设置

二.httpRequest-httpResponse-JsonResponse对象的更多相关文章

  1. Django day05 视图层之 (HttpRequest) \ (HttpResponse) \ (JsonResponse) 对象

    一:视图层之HttpRequest对象 # 前台Post传过来的数据,包装到POST字典中 # request.POST # 前台浏览器窗口里携带的数据,包装到GET字典中 # request.GET ...

  2. Django框架(九):视图(二) HttpRequest对象、HttpResponse对象

    1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...

  3. Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse

    一.Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse.命名空间:  System.Web程序集:  System.Web(在 System.Web ...

  4. django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统

    Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...

  5. Request对象和Response对象 JsonResponse对象 和 Django shortcut functions 和QueryDict对象

    request request属性 属性: django将请求报文中的请求行.头部信息.内容主体封装成 HttpRequest 类中的属性. 除了特殊说明的之外,其他均为只读的. ''' 0.Http ...

  6. [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility

    [摘]http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html HttpRuntime HttpRuntime公开了一个静态方法 U ...

  7. 运维开发笔记整理-JsonResponse对象

    运维开发笔记整理-JsonResponse对象 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用HttpResponse发送json格式的数据 1>.HttpRespo ...

  8. Django——20141014深入理解Django HttpRequest HttpResponse的类和实例

    深入理解Django HttpRequest HttpResponse的类和实例 了解META选项 了解中间件 理清所有模板传输模板变量的方式,并作出选择 Django模板系统:如何利用Django模 ...

  9. JavaScript学习总结(二)数组和对象部分

    pt学习总结(二)数组和对象部分 2016-09-16    分类:WEB开发.编程开发.首页精华暂无人评论     来源:trigkit4 分享到:更多1 对象部分 Object类型 Object  ...

  10. JsonResponse对象浅析

    JsonResponse   JsonResponse 对象: class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_ ...

随机推荐

  1. Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)

    650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...

  2. Java实现 LeetCode 537 复数乘法(关于数学唯一的水题)

    537. 复数乘法 给定两个表示复数的字符串. 返回表示它们乘积的字符串.注意,根据定义 i2 = -1 . 示例 1: 输入: "1+1i", "1+1i" ...

  3. Java实现 LeetCode 84 柱状图中最大得矩形

    84. 柱状图中最大的矩形 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的 ...

  4. protobuf安装流程

    protobuf安装流程 环境 平台 ​ Ubuntu16.04 依赖 autoconf automake libtool curl make g++ 安装流程 在Ubuntu / Debian上,您 ...

  5. 2.Redis 控制短信重发

    需要两个缓存 key名称 phone-busy,缓存1分钟key名称 phone-send-count,缓存1天,每成功发送一条+1 发送的时候流程如下: 判断phone-busy是否存在,存在直接报 ...

  6. 关联函数-web_save_param_length

    int web_save_param_length(const char * Param,const char * Base,LAST); 参数说明: Param:保存长度的参数的名称. Base:参 ...

  7. 腾讯音乐Android工程师一面面试题记录,拿走不谢!

    最近参加了一次鹅厂音乐Android工程师面试,这里凭记忆记录了一些一面的面试题,希望能帮到正在面试的你! 1.Java调用函数传入实际参数时,是值传递还是引用传递? 2.单例模式的DCL方式,为什么 ...

  8. Android学习笔记Log类输出日志信息

    Log类提供的方法 代码示例 .. Log.e(TAG,"[错误信息]"); Log.w(TAG,"[警告信息]"); Log.i(TAG,"[普通信 ...

  9. Spring系列.AOP原理简析

    Spring AOP使用简介 Spring的两大核心功能是IOC和AOP.当我们使用Spring的AOP功能时是很方便的.只需要进行下面的配置即可. @Component @Aspect public ...

  10. 使用vscode 开发go项目的最新姿势. go版本1.14.2

    使用了go 1.14.2. 版本, 再也不用建src, pkg, bin 目录了,   以及再也不用强制配置GOPATH了 前提条件: 必须是 go mod 项目. 在工程目录下, 执行这样的命令生成 ...