1,json 数据的获取
'''通过request.data来获取数据''' def unform(request): # request.body 返回的是 byte类型 byte_data = request.body #将byte 转换成字符串! str_data = byte_data.decode() #转换字典的形式! json_dir = json.loads(str_data) print(json_dir) print(request.method) print(request.path) return HttpResponse('ok')
3,请求头部信息的处理
def get_headers(request): # META 方法是获取请求的头部信息! print(request.META['CONTENT_TYPE']) return HttpResponse('OK')
4,Response 返回前端第一种形式!
def response(response): data = { 'one': 'hello' } respon = HttpResponse(data, status=400) #设置头部信息只能这样设置! respon['hello'] = 'hello world!' return respon
Response 返回前端第二种形式!
def response(response): data = { 'one': 'world' } respon = HttpResponse(data) respon.status_code = 404 respon['hello'] = 'hello world!' return respon
5,cookie 的设置与获取
def cookie(request): request.session['one'] = 'one' # cookie信息设置到 响应问价之中! response = HttpResponse('这是设置cookie信息!') # cookie 是键值对! response.set_cookie('key', 'value') # 获取 cookies 值,通过键! coo = request.COOKIES.get('key') print(coo) response.delete_cookie('key') print(coo) return response
6,SESSION 的设置与获取!
def session(request): # 使用了自动调用ression存储到redis request.session['one'] = 'two' data = request.session.get('one') print(data) # HttpResponse返回数据的时候自动将 session返回到浏览器之中! request.session.clear() # 清除session 信息只是删除值的部分! request.session.flush() # 清除session 键值对都要清除! return HttpResponse(data)