响应

视图在接收请求并处理后,必须返回HttpResponse对象或子对象。HttpRequest对象由Django创建,HttpResponse对象由开发人员创建。

1 HttpResponse

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

HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码)

也可通过HttpResponse对象属性来设置响应体、状态码:

  • content:表示返回的内容。
  • status_code:返回的HTTP响应状态码。

响应头可以直接将HttpResponse
response['sixstar'] = 'Python' # 自定义响应头sixstar, 值为Python

response = HttpResponse()
response['sixstar'] = 'Python'  # 自定义响应头sixstar, 值为Python

示例:

from django.http import HttpResponse
def demo_view(request):
    return HttpResponse('sixstar python', status=400)
    或者
    response = HttpResponse('sixstar python')
    response.status_code = 400
    response['sixstar'] = 'Python'
    return response

2 HttpResponse子类

Django提供了一系列HttpResponse的子类,可以快速设置状态码

  • HttpResponseRedirect 301
  • HttpResponsePermanentRedirect 302
  • HttpResponseNotModified 304
  • HttpResponseBadRequest 400
  • HttpResponseNotFound 404
  • HttpResponseForbidden 403
  • HttpResponseNotAllowed 405
  • HttpResponseGone 410
  • HttpResponseServerError 500

3 JsonResponse

若要返回json数据,可以使用JsonResponse来构造响应对象,作用:

  • 帮助我们将数据转换为json字符串
  • 设置响应头Content-Typeapplication/json
from django.http import JsonResponse
def demo_view(request):
    return JsonResponse({'city': 'beijing', 'subject': 'python'})

4 redirect重定向

from django.shortcuts import redirect
def demo_view(request):
    return redirect('/index.html')