对django views中 request, response的常用操作详解

yipeiwu_com5年前Python基础

request

获取post请求中的json数据

def hello(request):
 data = json.loads(request.body)
 ...

json格式还有一些 非表单序列化 的格式,都可以从 request.body 中获取请求体中的数据,对于ajax请求可以使用 request.is_ajax() 来判断

根据请求的信息获取base url(有时候服务的域名比较多,还是需要动态的拼接一下url信息)

# url http://wificdn.com:8888/wxpay/qrcode2/16122010404238801544?name=lzz
request.get_host() # wificdn.com:8888
request.get_full_path() # u'/wxpay/qrcode2/16122010404238801544?name=lzz'

request.build_absolute_uri('/') # 'http://wificdn.com:8888/'
request.build_absolute_uri('/hello') # 'http://wificdn.com:8888/hello'
request.build_absolute_uri() # 'http://wificdn.com:8888/wxpay/qrcode2/16122010404238801544?name=lzz'

request.path # u'/wxpay/qrcode2/16122010404238801544'
request.scheme # 'http'

获取表单中选中的 checkbox 信息, 例如checkbox的name为 checks

var_list = request.POST.getlist('checks')

返回的是个list对象,如果没有��️返回 [] ,如果表单中没有这个key也返回 []

response

json格式的响应 1.8版本中已经提供了 JsonResponse, from django.http import JsonResponse 就可以使用了,低版本的django可以参照源码自己写一个,几行代码就行了。 response 中设置 cookies 和 header

def xxxxview(request):
 ....

 resp = HttpResponseRedirect('/account/portal/?token=%s' % es)
 resp.set_cookie("coofilter", es, max_age=300)
 resp['Erya-Net-Type'] = NET_TYPE
 resp['Erya-Auth-Host'] = AUTH_HOST
 resp['Erya-Auth-Port'] = AUTH_PORT
 resp['Erya-Auth-Uip'] = ip
 resp['Erya-Auth-Token'] = es
 return resp

session

how to use session, 主要是get和set,和删除

def post_comment(request, new_comment):
 if request.session.get('has_commented', False):
 return HttpResponse("You've already commented.")
 c = comments.Comment(comment=new_comment)
 c.save()
 request.session['has_commented'] = True
 return HttpResponse('Thanks for your comment!')

def logout(request):
 try:
 del request.session['member_id']
 except KeyError:
 pass
 return HttpResponse("You're logged out.")

cookies

def login(request):
 response = HttpResponseRedirect('/url/to_your_home_page')
 response.set_cookie('cookie_name1', 'cookie_name1_value')
 response.set_cookie('cookie_name2', 'cookie_name2_value')
 return response

def logout(request):
 response = HttpResponseRedirect('/url/to_your_login')
 response.delete_cookie('cookie_name1')
 response.delete_cookie('cookie_name2')
 return response

# 获取
coo = request.COOKIES.get('coofilter')
# cookies 过期时间
hr.set_cookie('user_id', user_id, max_age=300)    

以上这篇对django views中 request, response的常用操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python的SimpleHTTPServer模块用处及使用方法简介

Python的SimpleHTTPServer模块用处及使用方法简介

  搭建FTP,或者是搭建网络文件系统,这些方法都能够实现Linux的目录共享。但是FTP和网络文件系统的功能都过于强大,因此它们都有一些不够方便的地方。比如你想快速共享Linux系统的...

Python实现远程调用MetaSploit的方法

本文较为详细的讲述了Python实现远程调用MetaSploit的方法,对Python的学习来说有很好的参考价值。具体实现方法如下: (1)安装Python的msgpack类库,MSF官...

Python selenium 三种等待方式详解(必会)

很多人在群里问,这个下拉框定位不到、那个弹出框定位不到…各种定位不到,其实大多数情况下就是两种问题:1 有frame,2 没有加等待。殊不知,你的代码运行速度是什么量级的,而浏览器加载渲...

python实现二级登陆菜单及安装过程

python实现二级登陆菜单及安装过程

python实现二级登陆菜单的代码如下所示: """ 1.三级菜单 注册 登陆 注销 2.进入每一个一级菜单,都会有下一级的菜单 """ user_item = dict() t...

python监控nginx端口和进程状态

本文实例为大家分享了python监控nginx端口和进程状态的具体代码,供大家参考,具体内容如下 #!/usr/local/bin/python # coding:utf-8 imp...