不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • python基础

  • python模块

  • django

    • 01_Django基础

      • 01.django介绍
      • 02.环境安装
      • 03.创建django项目
      • 04.路由配置
      • 05.请求与相应
        • 01.请求request
          • 1.1 常见request内容
          • 1.2 app01/views.py
          • 1.3 data.html
        • 02.相应
          • 2.1 HttpResponse
          • 2.2 JsonResponse
          • 2.3 Response
          • 2.4 render
          • 2.5 redirect
      • 06.类视图
      • 07.中间件
      • 08.cookie
      • 09.session
    • 02_Model数据库操作

    • 03_DRF框架

    • 04_部署

  • flask

  • SYL

  • Celery

  • 微服务

  • python
  • django
  • 01_Django基础
xiaonaiqiang
2021-02-15
目录

05.请求与相应

# 01.请求request

# 1.1 常见request内容

# 1、request.POST         # 获取post请求数据
# 2、request.GET          # 获取get请求
# 3、request.FILES        # 获取文件
# 4、request.getlist       # 获取表达中列表
# 5、request.method        # 获取请求方式 get/post
# 6、request.path_info      #获取当前url
# 7、request.body          #自己获取请求体数据
1
2
3
4
5
6
7

# 1.2 app01/views.py

def login(request):
    if request.method == 'GET':
        return render(request,'data.html')
    elif request.method == 'POST':
        v = request.POST.get('gender')      #1 获取单选框的value值
        v = request.POST.getlist('favor')   #2 获取多选框value:['11', '22', '33']
        v = request.POST.getlist('city')    #3 获取多选下拉菜单:['bj', 'sh', 'gz']
        print(v.name,type(v))


        #当服务器端取客户端发送来的数据,不会将数据一下拿过来而是一点点取(chunks就是文件分成的块)
        
        obj = request.FILES.get('fff')      #4 下面是获取客户端上传的文件(如:图片)
        import os
        file_path = os.path.join('upload',obj.name)
        f = open(file_path,mode='wb')
        for i in obj.chunks():
            f.write(i)
        f.close()

        return render(request,'login.html')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 1.3 data.html

<form action="/login/" method="post" enctype="multipart/form-data">
    <p>
        <input type="text" name="user" placeholder="用户名">
    </p>
    
    {# 1、单选框,返回单条数据的列表 #}
    <p>     
        男:<input type="radio" name="gender" value="1">
        女:<input type="radio" name="gender" value="2">
        人妖:<input type="radio" name="gender" value="3">
    </p>

    {# 2、多选框、返回多条数据列表 #}
    <p>     
        男:<input type="checkbox" name="favor" value="11">
        女:<input type="checkbox" name="favor" value="22">
        张扬:<input type="checkbox" name="favor" value="33">
    </p>

    {# 3、多选,返回多条数据的列表 #}
    <p>     
        <select name="city" multiple>
            <option value="bj">北京</option>
            <option value="sh">上海</option>
            <option value="gz">广州</option>
        </select>
    </p>

    {# 4、提交文件 #}
    <p><input type="file" name="fff"></p>
    <input type="submit" value="提交">
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 02.相应

# 2.1 HttpResponse

  • 需要手动将字符串转化成json字符串并相应到前端
  • 传到到前端的是json字符串,还需要手动进行转化
import json
from django.http import HttpResponse

def testResponse(request):
    data={
        'name':'zhangsan',
        'age':18,
    }
    # 默认格式:content_type="text/plain"
    return HttpResponse(json.dumps(data), content_type="application/json")
    # return HttpResponse(json.dumps(data), content_type="text/plain")
1
2
3
4
5
6
7
8
9
10
11

# 2.2 JsonResponse

  • JsonResponse继承HttpResponse
  • 数据类型装自动换成json字符串并相应到前端,传到前端的是数据类型而非json字符串
import json
from django.http import JsonResponse

def testResponse(request):
    data={
        'name':'张三',
        'age':18,
    }
    return JsonResponse(data=data)
1
2
3
4
5
6
7
8
9

# 2.3 Response

  • 是Django rest-framework框架中封装好的响应对象

  • 但是只能在继承于rest-framework的APIView的视图类中使用. 比较推荐.

  • 安装:pip install djangorestframework==3.9.2

from rest_framework.views import APIView

class TestResponse(APIView):
    def get(self,request,*args,**kwargs):
        data = {
            'name': '张三',
            'age': 18,
        }
        return Response({'name': 'zhangsan'})
1
2
3
4
5
6
7
8
9

# 2.4 render

  • 返回html页面
def index(request):
    return render(request, 'index.html',{'users':['zhangsan','lisi','wangwu']})
1
2

# 2.5 redirect

  • 重定向到新的页面
return redirect('https://www.baidu.com')
1
上次更新: 2024/3/13 15:35:10
04.路由配置
06.类视图

← 04.路由配置 06.类视图→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式