5, Django响应数据

连载, 接着上一篇
代码地址:
链接:https://pan.baidu.com/s/1K3bSI_QYqPYbCmjB2H147w 密码:ecgi

1, 为什么要有响应

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

2, 有哪些响应对象?

  • HttpResponse或者其子类都是
  • 子类:
    HttpResponseRedirect 301
    HttpResponsePermanentRedirect 302
    HttpResponseNotModified 304
    HttpResponseBadRequest 400
    HttpResponseNotFound 404
    HttpResponseForbidden 403
    HttpResponseNotAllowed 405
    HttpResponseGone 410
    HttpResponseServerError 500
    

3, 如何使用?

  • 这里就直贴关键视图了, 加群获取完整资料
    from django.http import HttpResponse
    
    def response_function(request):
    
    	# 方式一:
    	return HttpResponse(content='hello world', status=200)
    	
    	# 方式二:
    	# response = HttpResponse('hello world')
    	# response.status_code = 200
    	# response['name'] = 'zhangsan'
    	# return response
    

    HttpResponse原型
    HttpResponse(content=响应体, content_type=响应体, status=状态码)
    content:表示返回的内容
    status_code:返回的HTTP响应状态码

4, 如何响应Json数据?

from django.http import JsonResponse

def demo_view(request):
   return JsonResponse({'city': 'beijing', 'subject': 'python'})

若有不清楚的, 添加QQ群
免费答疑服务: 772044423

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值