国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Django學習筆記之View操作指南

瀏覽:4日期:2024-09-11 15:55:23
Django的View

一個視圖函數(類),簡稱視圖,是一個簡單的Python 函數(類),它接受Web請求并且返回Web響應。響應可以是一張網頁的HTML內容,一個重定向,一個404錯誤,一個XML文檔,或者一張圖片。

無論視圖本身包含什么邏輯,都要返回響應。代碼寫在哪里也無所謂,只要它在你當前項目目錄下面。除此之外沒有更多的要求了——可以說“沒有什么神奇的地方”。為了將代碼放在某處,大家約定成俗將視圖放置在項目(project)或應用程序(app)目錄中的名為views.py的文件中。

導入:from django.views import View

一、查詢所有數據

查詢數據在自定義的視圖類中定義get方法

使用django.http模塊中的JsonResponse對非json格式的數據做返回處理

在JsonResponse必須添加safe=False參數,否則會報錯:In order to allow non-dict objects to be serialized set the safe

from django.http import HttpResponse from django import http # Create your views here. class UserView(View): ’’’ 用戶視圖 ’’’ def get(self, request): # 模型類實例化對象 users = UserProfile.objects.all() user_list = [] for user in users: user_dict = { ’id’: user.id, ’username’: user.username, ’password’: user.password, ’open_id’: user.open_id, ’code’: user.code } user_list.append(user_dict) return http.JsonResponse(user_list) 二、創建數據

使用django中的json,把前端傳遞過來的json數據轉成字典

使用django.db.models模塊中的Q來查詢多個字段在數據庫中是否存在

from django.views import View from django.http import HttpResponse from django import http from django.db.models import Q import json class UserView(View): ’’’ 用戶視圖 ’’’ def post(self, request): # 獲取數據, json轉字典 dict_data = json.loads(request.body.decode()) print(dict_data) nick_name = dict_data.get(’nickName’) code = dict_data.get(’code’) open_id = 'xljsafwjeilnvaiwogjirgnlg' # 校驗數據 result = UserProfile.objects.filter(Q(code=code) | Q(open_id=open_id)) if not result.exists(): # 數據入庫 user = UserProfile.objects.create( username=nick_name, open_id=open_id, code=code ) # 返回響應 user_dict = { ’id’: user.id, ’username’: user.username, ’password’: user.password, ’open_id’: user.open_id, ’code’: user.code } return http.JsonResponse(user_dict) return http.JsonResponse('用戶已存在', safe=False, status=202)三、查詢某一條數據(單個)

前端需要傳遞pk/id值,通過pk/id查詢數據,查詢一條數據必須用get,不能用filter,否則會報錯:AttributeError: ’QuerySet’ object has no attribute ’id’

數據轉換

返回響應

class UserProfileDetail(View): ’’’ 詳情視圖 ’’’ def get(self, request): userInfo = UserProfile.objects.get(id=id) if not userInfo: return HttpResponse('查詢的用Info戶不存在', status=404) user_dict = { ’id’: userInfo.id, ’username’: userInfo.username, ’password’: userInfo.password, ’open_id’: userInfo.open_id, ’code’: userInfo.code } return http.JsonResponse(user_dict, status=200) 四、更新一條數據

前端需要傳遞pk/id值,通過pk/id查詢數據,查詢一條數據必須用get,不能用filter,否則會報錯:AttributeError: ’QuerySet’ object has no attribute ’id’

更新一條數據時必須使用filter來查詢數據集,再使用update(**data)來更新數據,不能使用get,否則會報錯:AttributeError: ’模型類’ object has no attribute ’update’

get查詢獲取到的是數據對象,而filter查詢獲取到的是數據集

class UserProfileDetail(View): ’’’ 詳情視圖 ’’’ def put(self, request, id): data_dict = json.loads(request.body.decode()) userInfo = UserProfile.objects.get(id=id) if not userInfo: return HttpResponse('查詢的用Info戶不存在', status=404) UserProfile.objects.filter(id=id).update(**data_dict) userInfo = UserProfile.objects.get(id=id) user_dict = { ’id’: userInfo.id, ’username’: userInfo.username, ’password’: userInfo.password, ’open_id’: userInfo.open_id, ’code’: userInfo.code } return http.JsonResponse(user_dict, status=200)五、刪除某一條數據

class UserProfileDetail(View): ’’’ 詳情視圖 ’’’ def delete(self, request, id): userInfo = UserProfile.objects.filter(id=id) if not userInfo: return HttpResponse('刪除的數據不存在', status=404)UserProfile.objects.filter(id=id).delete() return HttpResponse('數據刪除成功', status=204)

上述的操作只能適用于數據表中字段很少的情況,如果字段較多,寫起來會很麻煩,不利于開發

總結

到此這篇關于Django學習筆記之View操作指南的文章就介紹到這了,更多相關Django View操作內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Django
相關文章:
主站蜘蛛池模板: 欧美视频一区二区在线观看 | 精品欧美日韩一区二区三区 | 成人免费毛片一区二区三区 | 欧美高清性色生活片免费观看 | 亚洲天堂网在线观看 | 成人软件18免费网站 | 高清国产一区二区三区 | 夜色综合| 中国人免费观看高清在线观看二区 | 无国产精品白浆是免费 | 欧美高清视频手机在在线 | 久久久久久久久免费影院 | 日韩三级免费看 | 欧美成人性色生活片天天看 | a级毛片在线播放 | 国产精品爱久久久久久久小 | 免费一级a毛片在线播 | 久久一本色道综合 | 她也啪97在线视频 | 国产高清美女一级a毛片久久w | 精品国产午夜肉伦伦影院 | 中文字幕高清在线天堂网 | 世界一级毛片 | 免费国产成人午夜在线观看 | 国产一级二级三级毛片 | 成人久久久观看免费毛片 | 日韩精品中文字幕一区三区 | 日本一级毛片视频无遮挡免费 | 性感美女香蕉视频 | 久久爱wwwww 久久爱www成人 | 精品视频一区二区三区 | 久草在线免费资源站 | 亚洲免费在线视频播放 | 成人网视频在线观看免费 | 99久99久6久热在线播放 | 欧美成人a视频 | 欧美视频精品 | 中文字幕视频网 | 最刺激黄a大片免费观看 | 国产乱码精品一区二区三区卡 | 久久久久久久国产精品毛片 |