对django views中 request, response的常用操作详解
作者:orangleliu 时间:2021-02-21 15:00:51
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)
来源:https://blog.csdn.net/orangleliu/article/details/75226747
标签:django,views,request,response


猜你喜欢
javascript 函数式编程
2024-04-19 10:29:38
Ubuntu Server 20.04 LTS 环境下搭建vim 编辑器Python IDE的详细步骤
2023-06-02 05:13:18
mysql主键,外键,非空,唯一,默认约束及创建表的方法
2024-01-17 18:18:06
在python 中实现运行多条shell命令
2023-07-30 10:40:11
Python 抓取动态网页内容方案详解
2022-05-19 02:09:29

python后端接收前端回传的文件方法
2023-07-29 04:04:05
Element Carousel 走马灯的具体实现
2024-05-05 09:24:48

Pymysql实现往表中插入数据过程解析
2022-03-24 10:31:43

关于PyTorch 自动求导机制详解
2022-03-07 14:33:02

SQL为什么不建议执行超过3表以上的多表关联查询
2024-01-28 07:58:00

基于keras中import keras.backend as K的含义说明
2023-04-28 08:43:27
python 还原梯度下降算法实现一维线性回归
2023-10-09 21:53:42

Python标准库之typing的用法(类型标注)
2021-09-27 01:25:24

python在新的图片窗口显示图片(图像)的方法
2021-11-17 00:38:18

Sql Server查询性能优化之不可小觑的书签查找介绍
2012-05-22 18:24:53
sql server编写archive通用模板脚本实现自动分批删除数据
2024-01-18 03:27:11
Python并发编程实例教程之线程的玩法
2022-02-02 08:17:41
使用Python的Scrapy框架十分钟爬取美女图
2023-06-16 03:28:57

利用Python生成Excel炫酷图表
2023-03-09 21:52:00

python利用smtplib实现QQ邮箱发送邮件
2023-07-15 18:47:12
