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 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.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>

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")

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)

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'})

2.4 render

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

2.5 redirect

  • 重定向到新的页面
return redirect('https://www.baidu.com')
原文地址:https://www.cnblogs.com/xiaoxiamiaichiyu/p/14788747.html