Django HttpResponse 响应对象


目录


一、概述

所谓 HttpRequest 响应就是服务器返回给客户端的数据,HttpRequest 由程序员自己创建,一般他们通过两种方式来创建。

  1. 不使用模板,直接调用 HttpResponse(),返回 HttpResponse 对象;

  2. 通过 render() 函数调用模板(templates)进行渲染

    py 复制代码
    # render() 函数语法
    render(request, template_name, [context])
    
    # 函数参数说明
    # request:上面刚提到,即客户端的请求信息
    # template_name:模板名,如index.html
    # context:这是一个可选的字典类型参数,该参数用于传入模板中,从而更好地渲染模板

还有就是 HttpResponse 的一些子类也可实现响应。

  1. HttpResponseRedirect

    py 复制代码
    # 可实现服务器内部跳转(但还是推荐使用反向解析)
    return HttpResponseRedirect('/grade/2030')

    扩展:也可使用 redirect() 进行重定向,也就是说它与 HttpResponseRedirect 是等价的。

  2. JsonResponse

    py 复制代码
    # 返回Json数据的请求,通常用在异步请求上
    JsonResponse(dict)    # 将字典转为Json格式

二、测试

1、视图函数

py 复制代码
def my_response(request):
    # 1.返回字符串:企业项目中很少使用
    # return HttpResponse('ok')

    # 2.返回模板(用于前后端不分离的情况,即前后端代码放在一个工程下面,后端返回数据后,直接将数据渲染模板)
    # return render(request, 'index.html')

    # 3.重定向(一般用于页面跳转)
    # return redirect('/request/')
    # return HttpResponseRedirect('/request/')

    # 4.返回Json数据*(用于前后端不分离的情况,即后端只需返回数据给前端,前端拿到数据后自己去渲染)
    return JsonResponse({'data': 'hello'})

2、路由

py 复制代码
from django.urls import path
from user.views import index, my_request, my_response

urlpatterns = [
    path('user/', index, name='index'),
    path('request/', my_request, name='my_request'),
    path('response/', my_response, name='my_response'),
]

3、请求

三、属性和方法

一般很少用,常见属性和方法有一下几种:

1、属性

  • content:返回的内容;
  • charset:返回的编码格式;
  • status_code:返回的响应状态码。

2、方法

  • write():直接写出文本;
  • flush():冲刷缓冲区;
  • set_cookie(key, value='xxx', max_age=None):设置cookie;
  • delete_cookie(key):删除cookie。

四、解读 request 参数

**>>> 疑问?**来自 GPT 的回答

py 复制代码
# 1.视图函数(views.py)
from django.shortcuts import render

def index(request):
    # 渲染模板render,渲染HTML
    return render(request, 'index.html')
py 复制代码
# 2.模板文件(templates/index.html)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>首页</h2>
    <hr>
    <h4>Hello Django!</h4>
</body>
</html>
py 复制代码
# 3.路由(urls.py)
from django.contrib import admin
from django.urls import path
from user.views import *

urlpatterns = [
    path('index/', index),
    path('admin/', admin.site.urls),
]

因此,想这个案例流程就是:当客户端向我的 Django 项目 Web 服务发起 HTTP 请求时,Django 将获取客户端的请求信息,如请求方法GET、POST 及 URL 信息等(由 request 获取)。

  • 客户端请求:http://xxx/index/
  • Django 获取客户端请求的 URL 为 index
  • 于是路由到 Django 视图函数 index,即上面的 path('index/', index)
  • 而该函数是有模板渲染的,且模板文件为 index.html,即上面视图函数中 return render(request, 'index.html')
  • 因此,Django 通过 render() 函数渲染 index.html 后,将渲染的结果响应给客户端。

---END

相关推荐
luoluoal8 小时前
基于python的RSA算法的数字签名生成软件(源码+文档)
python·mysql·django·毕业设计
牢七1 天前
5655869
django
秋氘渔2 天前
智演沙盘 —— 基于大模型的智能面试评估系统
python·mysql·django·drf
jcsx3 天前
如何将django项目发布为https
python·https·django
百锦再3 天前
京东云鼎入驻方案解读——通往协同的“高架桥”与“快速路”
android·java·python·rust·django·restful·京东云
Warren984 天前
datagrip新建oracle连接教程
数据库·windows·云原生·oracle·容器·kubernetes·django
韩立学长4 天前
【开题答辩实录分享】以《跳蚤市场二手物品交易推荐平台》为例进行选题答辩实录分享
python·django
飞天小蜈蚣4 天前
django的ulr注意事项、模板渲染
python·django·sqlite
Q_Q5110082854 天前
python_django基于大数据技术旅游景点数据分析推荐系统现_wrqk1aes
大数据·python·django
心本无晴.5 天前
拣学--基于vue3和django框架实现的辅助考研系统
vue.js·python·mysql·考研·django·dify