HttpRequest对象


  • 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取
  • 查询字符串(query string),形如key1=value1&key2=value2
  • 请求体(body)中发送的数据,比如表单数据、json、xml
  • 在http报文的头(header)中

url 路径参数

位置参数

url(r'^(\d+)/(\d+)/$', views.index),

def index(request, value1, value2):
      context = {'v1':value1, 'v2':value2}
      return render(request, 'Book/index.html', context)

关键字参数

url(r'^(?P<value1>\d+)/(?P<value2>\d+)/$', views.index),

def index(request, value2, value1):
      context = {'v1':value1, 'v2':value2}
      return render(request, 'Book/index.html', context)

Django中的QueryDict对象

HttpRequest对象的属性GET、POST都是QueryDict类型的对象 与python字典不同,QueryDict类型的对象用来处理同一个键带有多个值的情况 方法get():根据键获取值 如果一个键同时拥有多个值将获取最后一个值 如果键不存在则返回None值,可以设置默认值进行后续处理 get('键',默认值)

方法getlist():根据键获取值,值以列表返回,可以获取指定键的所有值 如果键不存在则返回空列表[],可以设置默认值进行后续处理 getlist('键',默认值)

查询字符串

# /get/?a=1&b=2&a=3

def get(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')

请求体

表单类型

def post(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')

非表单类型

非表单类型的请求体数据,Django无法自动解析,可以通过request.body属性获取最原始的请求体数据,自己按照请求体格式(JSON、XML等)进行解析。request.body返回bytes类型。 例如要获取请求体中的如下JSON数据

{"a": 1, "b": 2}
import json

def post_json(request):
    json_str = request.body
    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:一个类似于字典的对象,包含所有的上传文件。