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可以保存狀態但注意不要存儲敏感信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章