第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会自动匹配成功的值,做为一个方法参数传递到视图函数中

代码参考:django

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

# 在项目下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中的查询字符串的值app

代码参考:ide

# 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')
相关文章
相关标签/搜索