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"htmlcode">
var_list = request.POST.getlist('checks')

返回的是个list对象,如果没有"htmlcode">

def xxxxview(request):
 ....

 resp = HttpResponseRedirect('/account/portal/"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的常用操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!