HttpResponse对象
概念
服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,视图函数的第一个参数是HttpRequest对象在django.http模块中定义了HttpRequest对象的API
特点
属性:path:一个字符串,表示请求的页面的完整路径,不包含域名method:一个字符串,表示请求使用的HTTP方法,常用值包括: 'GET'、 'POST 。encoding:一个字符串,表示提交的数据的编码方式,如果为None则表示使用浏览器的默认设置,一般为utf-8这个属性是可写的,可以通过修改它来修改访问表单数据使用的编码,接下来对属性的任何访问将使用新的encoding值GET: 一个类似于字典的对象,包含get请求方式的所有参数POST: 一个类似于字典的对象,包含post请求方式的所有参数FILES:一个类似于字典的对象,包含所有的上传文件COOKIES:一个标准的Python字典,包含所有的cookie,键和值都为字符串session:一个既可读又可写的类似于字典的对象,表示当前的会话,只有当Django启用会话的支持时才可用,详细内容见"状态保持"方法:is_ajax(): 如果请求是通过XMLHttpRequest发起的,则返回True
细节
前端响应与后端匹配的HTTP请求方式要一致
表单标签
在HTML中,form表单的作用是收集标签中的内容,<form... </form>中间可以由访问者添加类似于文本,选择,或者一些控制模块等等,然后这些内容将会被送到服务端。一个表单必须指定两样东西:1. form的method参数用于设置表单的提交方式,默认使用POST.2.action用于设置表单的提交url,如果不写或者保持空字符串,那么将使用当前的URL.
表单标签的示例代码
<form action=" /blog/ get_ test1" method=" get" >a: < i nput type=" text name=" a > < br>b: < input type=" text name=" b~ > < br >< input type=" submi t”value=" 提交"></ form>
视图渲染H5文件
def req_ test (request) :print (request. path)print (request. method)return render (request,' tsl1/get_ form. html' )def get_ test (request) :a = request. GET. get( a' )b = request. GET. get( b' )print(a, b)return Ht tpResponse 11111)
Get 和 Post方法的区分
1.get提交的参数会在url中显示.2.可以通过request.GET.get的方法来获取提交的参数.
Get和Post请求
Get属性
1.-QueryDict类型的对象2.-包含get请求方式的所有参数3.-与url请求地址中的参数对应,位于?后面4.-参数的格式是键值对,如key1 =value1多个参数之间,使用&连接,如 key1 = value1https://www.baidu.com/s?ie=utf-8&word=%E6%B7%98%E5%AE%9D
Post属性
1.-QueryDict类型的对象2.-包含post请求方式的所有参数3.-与form表单中的控件对应4.-表单中控件要有name属性,则name属性的值为键,value 属性的值为值,构成键值对提交5.-对于checkbox控件,name 属性一样为一组,当控件被选中后会被提交,存在一-键多值的情况.
关于二者的总结
1. GET:GET如其名,是从服务器获取数据,不会更改服务器的状态和数据,在URL中携带参数发送给服务器。2. POST则是将一定量的数据发送给服务器,一般会更改服务器的数据。3. POST方法的参数不能在URL当中看到,他是通过body参数传递给服务器的,所以相对GET方法直接能在URL当中看到传递的参数,显得更加安全一些。当然也不能简单的判定POST方法比GET方法更安全,要使网站保持安全需要做更多的安全处理。
类视图
示例代码—-视图编写逻辑
from django. views import Viewclass BlogAdd (View) :def get(self, request) :return render (request,' blogs/demo_ add. html' )def post (self, request) :title = request. POST. get(' title' )content = request. POST[content' ]blog = BlogModel (title=title, content= content)blog. save()return redirect (reverse( cls_ add' ))
代码实战—-1(浏览器提交Get请求的数据)
### HTML5<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>测试Views视图函数</title></head><body><form action="#" method="get">a: <input type="text" name="a"><br>b: <input type="text" name="b"><br></form><input type="submit" value="提交"></body></html>### views.pyfrom django.http import HttpResponsedef get_test(request):a = request.GET.get('a')b = request.GET.get('b')print(a, b)return HttpResponse("测试GET方法")### urls.pypath('get_test/', views.get_test), # 测试Get方法
效果
代码实战—-2(渲染提交表单)
### views.pydef req_test(request):print(request.path)print(request.method)return render(request, "viewstest.html")### urls.pypath('req_test/', views.req_test), # 测试Get方法,渲染整个HTML5
效果
代码实战—-3同时处理多种请求
### views.pydef post_test(request):if request.method == 'GET':return render(request, 'viewstest.html')elif request.method == 'POST':a = request.POST.get('a')b = request.POST.get('b')print(a, b)return HttpResponse('')else:return HttpResponse("这个不是一个可以处理的请求")### urls.pypath('post_test/', views.post_test), # 测试Get方法,同时处理两种请求
使用django.db.Views书写请求
### views.pyclass BlogAdd(View):def get(self, request):return render(request, 'viewstest.html')def post(self, request):title = request.POST.get('title')content = request.POST.get('content')return HttpResponse('post请求。')### urls.pypath('clas_add/', views.BlogAdd.as_view(), name = "clas_add"), # django.db.Views重构
使用postman监听
辅助工具___postman
### 下载地址https://www.postman.com/downloads/
使用详情
文件上传
代码块
### settings.py# 设置上传文件夹地址STATIC_URL = '/static/'STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')### views.pyimport osfrom django.shortcuts import renderfrom django.http import HttpResponsefrom django.views import Viewfrom tzlook.settings import MEDIA_ROOTdef upload_test(request):if request.method == 'GET':return render(request, 'ss.html')elif request.method == 'POST':fl = request.FILES('file')fl.name = os.path.join(MEDIA_ROOT, fl.name)with open(fl.name, 'wb') as f:for c in fl.chunks():f.write(c)return HttpResponse('成功读取,已结束。')else:return HttpResponse('ERROR')### urls.pypath('upload_test/', views.upload_test, name = "upload_test"), # 文件上传
效果
总结
content 返回的是一个字符串类型
charset 字符串类型 编码的字符集
post比get安全,比如我们的敏感信息不能放入get里面
