第3天:视图提取请求参数和响应对象

  • 客户端传参的几种方式
  • 获取URL路径中的参数
  • 获取查询字符串参数
  • 获取请求体数据
  • 获取请求头数据
  • 响应

客户端传参的几种方式

  1. 通过URL路径传递,例如:http://127.0.0.1:8000/news/1/2,两个参数:新闻类别id和页码
  2. 通过query string查询字符串传递,例如:http://127.0.0.1:8000/news?category=1&page=2
  3. 通过body请求体传递,又可根据数据格式,分为 键值对,表单数据,非表单数据(json,xml)
  4. 通过http协议请求头(header)传递

获取URL路径中的参数

需求:

  1. 在服务器端视图中,获取URL路径传递过来的参数
  2. 例如:对于请求URL http://127.0.0.1:8000/news/1/2,需要获取URL路径中的数值1(类别id)和2(页码)

解决:

  1. 在配置URL时,可以使用正则表达式匹配URL中的参数
  2. 需要使用小括号把要匹配的值变为正则的一个组,可以对组命名,也可以不命名
  3. 当匹配成功后,Django会自动匹配成功的值,作为一个方法参数传递到视图函数中

代码参考:

  ①未命名参数(位置参数):按定义顺序传递

# 在项目下urls.py文件中配置
url(r'^news/(d+)/(d+)$', users.views.news),

#在users.views.py中定义
def news(request, a, b):
    return HttpResponse('显示新闻:{} {}'.format(a, b))

  ②命名参数(关键字参数):按定义的组名传递

# 在项目下的urls.py文件中配置
url(r'^news/(?P<category>d+)/(?P<page>d+)$', users.views.news),

#在users.views.py文件中定义
def news(request, category, page):
    return HttpResponse('显示新闻:{} {}'.format(category, page))

获取查询字符串参数

需求:

  获取URL地址 http://127.0.0.1:8000/news?categeory=1&page=2中的查询字符串的值

代码参考:

# url配置
url(r'^news/$', users.views.news),

# 视图函数
def news(request):
    category = request.GET.get('category')
    page = request.GET.get('page')
    text = '获取查询字符串:<br/> category={}, page={}'.format(category, page)
    return HttpResponse(text)

重要:查询字符串不却分请求方式,即使客户端通过POST方式发起请求,依然可以通过request.GET获取请求中的查询字符串数据

获取请求体数据

请求体数据格式不固定,可以是表单类型字符串,可以是JSON字符串,可以是XML字符串,应区别对待。

可以发送的请求体数据的 请求方式有POSTPUTPATCHDELETE

Django对POST、PUT、PATCH、DELETE请求方式开启了CSRF安全防护,为了方便测试,可以在settings.py文件中注释掉CSRF中间件,关闭CSRF防护

获取表单数据 Form Data()键值对

前端发送的表单或键值对类型的请求体数据,可以通过request.POST属性获取

# url配置
url(r'^news/$', users.views.news),

# 视图函数
def news(request):
    category = request.POST.get('category')
    page = request.POST.get('page')
    text = '获取body中的键值对:<br/> category={}, page={}'.format(category, page)
    return HttpResponse(text)

使用Postman发送表单数据

重要:request.POST只能用来获取POST方式的请求体表单数据或键值对数据。如果为非请求提交的请求体数据,或者是请求体数据类型为非表单或非键值对数据,则需要通过request.body属性获取提交的数据后自己解析

非表单类型None-Form Data

例如: 获取请求体中的json数据 {"category": 1, "page": 2}

# url配置
url(r'^news/$', users.views.news),

# 视图函数
def news(request):
    # 获取JSON字符串
    json_str = request.body

    #解析json
    dict_data = json.loads((json_str))
    category = dict_data.get('category')
    page = dict_data.get('page')
    text = '获取body中的json数据:<br/> category={}, page={}'.format(category, page)
    return HttpResponse(text)

获取请求头数据

可以通过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).
创建的请求头

注意: 获取自定义的请求头属性值时,需要添加前缀HTTP_并转换成大写,作为键来获取值

# url配置
url(r'^news/$', users.views.news),

# 视图函数
def news(request):
    a = request.META.get('HTTP_A')
    b = request.META.get('HTTP_B')

    text = '获取headers的数据:<br/> a={}, b={}'.format(a, b)
    return HttpResponse(text)

响应

视图必须返回一个HttpResponse对象(或其子类), 可以将要返回的字符串数据传给HttpResponse对象再返回

HttpRequst对象由Django创建,HttpResponse对象由开发人员创建

HttpResponse的常用子类

  • HttpResponseRedirect 重定向
  • JsonResponse 返回json数据

HttpResponse

  可以使用django.http.HttpResponse来构造响应对象

def news(request):
    # HttpResponse(content=响应体, content_type=响应体数据MIME类型, status=状态码)
    json_str = '{"name": "heboan"}'
    return HttpResponse(json_str, content_type='application/json', status=400)

  响应头设置:可以直接将HttpResponse对象当做字典进行响应头键值对的设置

def news(request):
    # HttpResponse(content=响应体, content_type=响应体数据MIME类型, status=状态码)
    response = HttpResponse('my name is heboan')
    response['name'] = 'heboan'
    return response

JsonResponse对象

  帮助我们将数据转换为json字符串,再返回给客户端,会自动设置响应头Conten-Type为application/json

from django.http import HttpResponse, JsonResponse
..

def news(request):
    return JsonResponse({'name':'heboan'})

  JsonResponse可以接收非字典数据,需要指定safe=False

redirect重定向

重定向:不返回具体显示内容给客户端,让客户端重新请求返回的地址,获取内容

from django.shortcuts import redirect
...

def news(request):
    return redirect('/index')
原文地址:https://www.cnblogs.com/sellsa/p/10337399.html