助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
[TOC] 利用HTTP协议向服务器传参有以下几种方式 1. 从url中截取 2. 使用查询字符串 3. 在请求体中发送数据 4. 在http报文头 `header` 中发送数据 ## 1. 从url中获取截取 在定义路由规则的时候,可以使用正则表达式截取数据,然后传到视图函数中,在视图函数中使用参数接收。 例: 浏览器向后端发送了一个请求,url为 `/weather/beijing/20171001` - 未命名参数 路由 ```python url(r'^weather/([a-z]+)/(\d{8})$', views.weather, name="weather") ``` ```python def weather(request, city, date): content = city+date return HttpResponse(content) ``` 视图函数要使用位置参数接收,位置要对应,响应结果 `beijing20171001` - 命名参数 ```python url(r'^demo/', include("demo.urls", namespace="demo")) ``` ```python def weather(request, date, city): content = city+date return HttpResponse(content) ``` 可以使用正则表达式给参数命名,参数接收的时候位置不需要照应,响应结果 `beijing20171001` - 从url中截取参数不区分 get post请求方式。 ## 2. QueryDict 在django的 `HttpRequest` 对象中,属性 `GET` 和 `POST` 得到的都是 `django.http.QueryDict` 所创建的实例,这是django自定义的一个类似字典的类。 在Python的字典中,一个键只能有一个值,当一个键赋值多次的时候,只会保留最后一个值。而在 `HttpRequest` 对象中,一个键往往有多个值。而 `QueryDict` 就可以用来处理一个键带多个值的情况。 ### (1). QueryDict.get(key, [default]) ```python QueryDict.get(key, [default]) ``` 返回 key 的值。如果key 具有多个值,只返回最后(最新)的值。当key 不存在时返回一个默认值。       ### (2). QueryDict.getlist(key, [default]) ```python QueryDict.getlist(key, [default]) ``` 返回 key 的值。如果key 具有多个值,以列表形式返回 key 的所有值。当key 不存在时返回一个空列表,设置了默认值则返回默认值。 ## 3. 查询字符串 在url中格式如 `?a=1&b=2` 的字符串叫做查询字符串,可以用过 `request.GET` 获取, `request.GET` 返回值是 `QueryDict` 对象。 url ``` /query?a=1&b=2&b=3 ``` 路由 ```python url(r'^query', views.query, name="query") ``` 视图函数 ```python def query(request): query_dict = request.GET a = query_dict.get('a') b = query_dict.getlist('b') context = "a=%s,b=%s" % (a, b) return HttpResponse(context) ``` 响应结果 ``` a=1,b=['2', '3'] ``` - 查询字符串不区分 get post 请求方式。 ## 4. 请求体 请求体获得参数的各种有多种,例如 表单,json,xml...不同格式的数据要区别对待 可以发送请求体数据的请求方式有POST、PUT、PATCH、DELETE。 django默认开启了csrf防护,会对上述的请求方式做验证,测试时可以关闭验证。 打开项目的配置文件 `settings.py` 注释掉 scrf中间件 ```python MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ``` ### 4.1 Form表单 前端发送的表单类型的数据,可以使用 `request.POST` 获取, `request.POST` 返回值是 `QueryDict` 对象。 url: ``` demo/form ``` 路由 ``` url(r'^form', views.form, name="form") ``` 表单数据 ![](https://box.kancloud.cn/36245d5a365d6bf8417297c63e21b39b_462x92.png) 视图函数 ```python def form(request): form_data = request.POST name = form_data.get("name") age = form_data.get("age") context = "name=%s,age=%s" % (name, age) return HttpResponse(context) ``` 响应结果 ``` name=xuanli,age=18 ``` ### 4.2 非表单类型 非表单类型的数据django无法自动解析,需要通过 `request.body` 获取原始的请求体数据,然后自己根据数据类型解析。 `request.body` 返回值是 `bytes` 对象。 例如前端请求的是json数据: url ``` demo/json ``` 路由 ``` url(r'^json', views.get_json, name="json") ``` json数据 ``` {"name": "xuanli", "age": "18"} ``` 视图函数 ```python def get_json(request): json_data = request.body json_data = json_data.decode() req_data = json.loads(json_data) name = req_data.get("name") age = req_data.get("age") context = "name=%s,age=%s" % (name, age) return HttpResponse(context) ``` 响应结果 ``` name=xuanli,age=18 ``` - python3.6不需要使用 `decode()` 解码 ## 请求头 ``` ``` ## 其他