django请求与响应中的类试图 以及文件上传 HttpResponse对象

类视图: View类视图的引用.在url.py中配置路由是通过: as_view()

from django.views import View
#类试图
class getTest(View):
    def get(self,request):
        if request.method == 'GET':
            username = request.GET.getlist('username', 123)  # default 默认值
            password = request.GET.get('password')
            print(username)
            print(password)
            return render(request, 'boke/get_test.html')

    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        print(username)
        print(password)
        return HttpResponse(123)

urls.py文件中配置

path('getTest/',views.getTest.as_view(),name='getTest'),

2-文件上传:

Django在处理文件上传的时候,文件数据被保存在了request.FILES
FILES中的每个键为中的name

1-设置文件的存储路径

1.在项目根目录下static中创建media文件夹
2.图片上传后,会被保存到“/static/media/文件”
3.打开settings.py文件,增加media_root项

STATIC_URL = '/static/'
STATICFILES_DIRS=[
        os.path.join(BASE_DIR, 'static')
]


MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

2-文件上传form表单中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>get_test测试</title>
</head>
<body>
<a href="/boke/get_test?username=qwe&username=abc&password=123456">嘿嘿嘿</a>
<form action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    传送文件:<input type="file" name="file">
    用户名: <input type="text" placeholder="请输入用户名" name="username"><br>
    密码 <input type="password" name="password"><br>
    <input type="submit" value="发布">
</form>
</body>
</html>

注意:FILES只有在请求的方法为POST 且提交的带有enctype=“multipart/form-data” 的情况下才会包含数据。否则,FILES 将为一个空的类似于字典的对象

3-文件上传视图函数

from django.views import View
from boke_demo.settings import MEDIA_ROOT #文件目录
import os

class getTest(View):
    def get(self,request):
        if request.method == 'GET':
            username = request.GET.getlist('username', 123)  # default 默认值
            password = request.GET.get('password')
            print(username)
            print(password)
            return render(request, 'boke/get_test.html')

    def post(self,request):
        file = request.FILES.get("file")
        file_name = os.path.join(MEDIA_ROOT, file.name)
        with open(file_name, "wb") as f:
            for i in file.chunks():
                f.write(i)
        return HttpResponse("上传成功")

结果:
在这里插入图片描述
在这里插入图片描述
上传后存储在media文件夹下面的照片成功打开
在这里插入图片描述

3-HttpResponse对象

在这里插入图片描述

1-HttpResponse的子类

返回数据的响应函数有:
HttpResponse() 返回简单的字符串对象
render() 渲染模板
redirect() 重定向
JsonResponse() 返回json数据

在这里插入图片描述

  • 帮助用户创建JSON编码的响应
  • 参数data是字典对象
  • JsonResponse的默认Content-Type为application/json

4-HTTP协议

在这里插入图片描述
HTTP(超文本传输协议)是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型。HTTP是一个无状态的协议。

1-客户端和服务器都是怎么记录登录的状态—通过cookie与session

cookie在客户端记录
session在服务端记录

#设置cookie
def get_ck(request):
    response = HttpResponse("来了")
    response.set_cookie("name","sang")  #不能出现中文
    return response

在这里插入图片描述
在这里插入图片描述
注意:
虽然cookie可以保存状态 但注意不要存储敏感信息

猜你喜欢

转载自blog.csdn.net/qq_42662411/article/details/104714639
今日推荐