django基础:请求与响应 | django |《python学习之路》| python 技术论坛-380玩彩网官网入口
请求
简介
回想一下,利用http协议向服务器传参有几种途径?
- 提取url的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取;
- 查询字符串(query string),形如key1=value1&key2=value2;
- 请求体(body)中发送的数据,比如表单数据、json、xml;
- 在http报文的头(header)中。
url路径参数
在定义路由url时,可以使用正则表达式提取参数的方法从url中获取请求参数,django会将提取的参数直接传递到视图的传入参数中。
未命名参数按定义顺序传递, 如
url(r'^weather/([a-z] )/(\d{4})/$', views.weather), def weather(request, city, year): print('city=%s' % city) print('year=%s' % year) return httpresponse('ok')
命名参数按名字传递,如
url(r'^weather/(?p
[a-z] )/(?p , views.weather), def weather(request, year, city): print('city=%s' % city) print('year=%s' % year) return httpresponse('ok')\d{4})/$'
django中的querydict对象
定义在django.http.querydict,httprequest对象的属性get、post都是querydict类型的对象,与python字典不同,querydict类型的对象用来处理同一个键带有多个值的情况
方法
get():根据键获取值
如果一个键同时拥有多个值将获取最后一个值
如果键不存在则返回none值,可以设置默认值进行后续处理
dict.get('键',默认值) 可简写为 dict['键']
getlist():根据键获取值,值以列表返回,可以获取指定键的所有值
如果键不存在则返回空列表[],可以设置默认值进行后续处理
dict.getlist('键',默认值)
查询字符串query string
获取请求路径中的查询字符串参数(形如?k1=v1&k2=v2),可以通过request.get属性获取,返回querydict对象。
# /qs/?a=1&b=2&a=3
def qs(request):
a = request.get.get('a')
b = request.get.get('b')
alist = request.get.getlist('a')
print(a) # 3
print(b) # 2
print(alist) # ['1', '3']
return httpresponse('ok')
{{< admonition warning “注意” true >}}
查询字符串不区分请求方式,即假使客户端进行post方式的请求,依然可以通过request.get获取请求中的查询字符串数据。
{{< /admonition >}}
请求体
简介
请求体数据格式不固定,可以是表单类型字符串,可以是json字符串,可以是xml字符串,应区别对待。
可以发送请求体数据的请求方式有post、put、patch、delete。
django默认开启了csrf防护,会对上述请求方式进行csrf防护验证,在测试时可以关闭csrf防护机制,方法为在settings.py文件中注释掉csrf中间件,如:
表单类型 form data
前端发送的表单类型的请求体数据,可以通过request.post属性获取,返回querydict对象。
def get_body(request):
a = request.post.get('a')
b = request.post.get('b')
alist = request.post.getlist('a')
print(a)
print(b)
print(alist)
return httpresponse('ok')
{{< admonition warning “注意” true >}}
request.post只能用来获取post方式的请求体表单数据
{{< /admonition >}}
非表单类型 non-form data
非表单类型的请求体数据,django无法自动解析,可以通过request.body属性获取最原始的请求体数据,自己按照请求体格式(json、xml等)进行解析。request.body返回bytes类型。
例如要获取请求体中的如下json数据
{"a": 1, "b": 2}
可以进行如下方法操作:
import json
def get_body_json(request):
json_str = request.body
json_str = json_str.decode() # python3.6 无需执行此步
req_data = json.loads(json_str)
print(req_data['a'])
print(req_data['b'])
return httpresponse('ok')
请求头
可以通过request.meta属性获取请求头headers中的数据,request.meta为字典类型。
常见的请求头如:
content_length
– the length of the request body (as a string).content_type
– the mime type of the request body.http_accept
– acceptable content types for the response.http_accept_encoding
– acceptable encodings for the response.http_accept_language
– acceptable languages for the response.http_host
– the http host header sent by the client.http_referer
– the referring page, if any.http_user_agent
– the client’s user-agent string.query_string
– the query string, as a single (unparsed) string.remote_addr
– the ip address of the client.remote_host
– the hostname of the client.remote_user
– the user authenticated by the web server, if any.request_method
– a string such as"get"
or"post"
.server_name
– the hostname of the server.server_port
– the port of the server (as a string).
具体使用如:
def get_headers(request):
print(request.meta['content_type'])
return httpresponse('ok')
其他常用httprequest对象属性
method:一个字符串,表示请求使用的http方法,常用值包括:’get’、’post’。
user:请求的用户对象。
path:一个字符串,表示请求的页面的完整路径,不包含域名和参数部分。
encoding:一个字符串,表示提交的数据的编码方式。
- 如果为none则表示使用浏览器的默认设置,一般为utf-8。
- 这个属性是可写的,可以通过修改它来修改访问表单数据使用的编码,接下来对属性的任何访问将使用新的encoding值。
files:一个类似于字典的对象,包含所有的上传文件。
响应
简介
视图在接收请求并处理后,必须返回httpresponse对象或子对象。httprequest对象由django创建,httpresponse对象由开发人员创建。
httpresponse
可以使用django.http.httpresponse来构造响应对象。
httpresponse(content=响应体, content_type=响应体数据类型, status=状态码)
也可通过httpresponse对象属性来设置响应体、状态码:
- content:表示返回的内容。
- status_code:返回的http响应状态码。
响应头可以直接将httpresponse对象当做字典进行响应头键值对的设置:
response = httpresponse()
response['itcast'] = 'python' # 自定义响应头itcast, 值为python
示例:
from django.http import httpresponse
def demo_view(request):
return httpresponse('itcast python', status=400)
或者
response = httpresponse('itcast python')
response.status_code = 400
response['itcast'] = 'python'
return response
httpresponse子类
django提供了一系列httpresponse的子类,可以快速设置状态码
- httpresponseredirect 301
- httpresponsepermanentredirect 302
- httpresponsenotmodified 304
- httpresponsebadrequest 400
- httpresponsenotfound 404
- httpresponseforbidden 403
- httpresponsenotallowed 405
- httpresponsegone 410
- httpresponseservererror 500
jsonresponse
若要返回json数据,可以使用jsonresponse来构造响应对象,作用:
- 帮助我们将数据转换为json字符串
- 设置响应头content-type为 application/json
from django.http import jsonresponse
def demo_view(request):
return jsonresponse({'city': 'beijing', 'subject': 'python'})
redirect重定向
from django.shortcuts import redirect
def demo_view(request):
return redirect('/index.html')
cookie
简介
cookie,有时也用其复数形式cookies,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)。cookie最早是网景公司的前雇员lou montulli在1993年3月的发明。cookie是由服务器端生成,发送给user-agent(一般是浏览器),浏览器会将cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该cookie给服务器(前提是浏览器设置为启用cookie)。cookie名称和值可以由服务器端开发自己定义,这样服务器可以知道该用户是否是合法用户以及是否需要重新登录等。服务器可以利用cookies包含信息的任意性来筛选并经常性维护这些信息,以判断在http传输中的状态。cookies最典型记住用户名。
cookie是存储在浏览器中的一段纯文本信息,建议不要存储敏感信息如密码,因为电脑上的浏览器可能被其它人使用。
特点
- cookie以键值对的格式进行信息的存储。
- cookie基于域名安全,不同域名的cookie是不能互相访问的,如访问itcast.cn时向浏览器中写了cookie信息,使用同一浏览器访问baidu.com时,无法访问到itcast.cn写的cookie信息。
- 当浏览器请求某网站时,会将浏览器存储的跟网站相关的所有cookie信息提交给网站服务器。
设置
可以通过httpresponse对象中的set_cookie方法来设置cookie。
httpresponse.set_cookie(cookie名, value=cookie值, max_age=cookie有效期)
- max_age 单位为秒,默认为none。如果是临时cookie,可将max_age设置为none。
示例:
def demo_view(request):
response = httpresponse('ok')
response.set_cookie('itcast1', 'python1') # 临时cookie
response.set_cookie('itcast2', 'python2', max_age=3600) # 有效期一小时
return response
读取
可以通过httprequest对象的cookies属性来读取本次请求携带的cookie值。request.cookies为字典类型。
def demo_view(request):
cookie1 = request.cookies.get('itcast1')
print(cookie1)
return httpresponse('ok')
session
启用session
django项目默认启用session。
可以在settings.py文件中查看
如需禁用session,将上图中的session中间件注释掉即可。
存储方式
在settings.py文件中,可以设置session数据的存储方式,可以保存在数据库、本地缓存等。
数据库
存储在数据库中,如下设置可以写,也可以不写,这是默认存储方式。
session_engine='django.contrib.sessions.backends.db'
如果存储在数据库中,需要在项installed_apps中安装session应用。
数据库中的表,表结构如下
由表结构可知,操作session包括三个数据:键,值,过期时间。
本地缓存
存储在本机内存中,如果丢失则不能找回,比数据库的方式读写更快。
session_engine='django.contrib.sessions.backends.cache'
混合存储
优先从本机内存中存取,如果没有则从数据库中存取。
session_engine='django.contrib.sessions.backends.cached_db'
redis
在redis中保存session,需要引入第三方扩展,我们可以使用django-redis来解决。
安装扩展
pip install django-redis
配置,在settings.py文件中做如下设置
caches = { "default": { "backend": "django_redis.cache.rediscache", "location": "redis://127.0.0.1:6379/1", "options": { "client_class": "django_redis.client.defaultclient", } } } session_engine = "django.contrib.sessions.backends.cache" session_cache_alias = "default"
如果redis的ip地址不是本地回环127.0.0.1,而是其他地址,访问django时,可能出现redis连接错误。
解决方法:修改redis的配置文件,添加特定ip地址。
打开redis的配置文件
sudo vim /etc/redis/redis.conf
在如下配置项进行修改(如要添加192.168.1.1地址)
重新启动redis服务
sudo service redis-server restart
操作
通过httprequest对象的session属性进行会话的读写操作。
以键值对的格式写session。
request.session['键']=值
根据键读取值。
request.session.get('键',默认值)
清除所有session,在存储中删除值部分。
request.session.clear()
清除session数据,在存储中删除session的整条数据。
request.session.flush()
删除session中的指定键及值,在存储中只删除某个键及对应的值。
del request.session['键']
设置session的有效期
request.session.set_expiry(value)
- 如果value是一个整数,session将在value秒没有活动后过期。
- 如果value为0,那么用户session的cookie将在用户的浏览器关闭时过期。
- 如果value为none,那么session有效期将采用系统默认值,默认为两周,可以通过在settings.py中设置session_cookie_age来设置全局默认值。