request对象常用方法
第一个参数HttpRequest,可以通过它获取很多信息,常用的信息有:
属性/方法说明 举例
request.path除域名以外的请求路径,以正斜杠开头"/hello/"
request.get_host()主机名(比如,通常所说的域名)"127.0.0.1:8000" or "www.example.com"
request.get_full_path()请求路径,可能包含查询字符串"/hello/?print=true"
request.is_secure()如果通过HTTPS访问,则此方法返回True, 否则返回False True 或者 False1
META信息,需要用get方式获取, 否则当字段不存在的时候会报错
request.META.get('HTTP_USER_AGENT','unknown')
第二个参数HttpResponse,输出图片文件
fromdjango.http import HttpResponse
defmy_image(request):
image_data= open("/path/to/my/image.png", "rb").read()
returnHttpResponse(image_data, mimetype="image/png")