久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Django學(xué)習(xí)筆記之View操作指南

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

一個(gè)視圖函數(shù)(類),簡(jiǎn)稱視圖,是一個(gè)簡(jiǎn)單的Python 函數(shù)(類),它接受Web請(qǐng)求并且返回Web響應(yīng)。響應(yīng)可以是一張網(wǎng)頁(yè)的HTML內(nèi)容,一個(gè)重定向,一個(gè)404錯(cuò)誤,一個(gè)XML文檔,或者一張圖片。

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

導(dǎo)入:from django.views import View

一、查詢所有數(shù)據(jù)

查詢數(shù)據(jù)在自定義的視圖類中定義get方法

使用django.http模塊中的JsonResponse對(duì)非json格式的數(shù)據(jù)做返回處理

在JsonResponse必須添加safe=False參數(shù),否則會(huì)報(bào)錯(cuò):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): # 模型類實(shí)例化對(duì)象 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) 二、創(chuàng)建數(shù)據(jù)

使用django中的json,把前端傳遞過(guò)來(lái)的json數(shù)據(jù)轉(zhuǎn)成字典

使用django.db.models模塊中的Q來(lái)查詢多個(gè)字段在數(shù)據(jù)庫(kù)中是否存在

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): # 獲取數(shù)據(jù), json轉(zhuǎn)字典 dict_data = json.loads(request.body.decode()) print(dict_data) nick_name = dict_data.get(’nickName’) code = dict_data.get(’code’) open_id = 'xljsafwjeilnvaiwogjirgnlg' # 校驗(yàn)數(shù)據(jù) result = UserProfile.objects.filter(Q(code=code) | Q(open_id=open_id)) if not result.exists(): # 數(shù)據(jù)入庫(kù) user = UserProfile.objects.create( username=nick_name, open_id=open_id, code=code ) # 返回響應(yīng) 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)三、查詢某一條數(shù)據(jù)(單個(gè))

前端需要傳遞pk/id值,通過(guò)pk/id查詢數(shù)據(jù),查詢一條數(shù)據(jù)必須用get,不能用filter,否則會(huì)報(bào)錯(cuò):AttributeError: ’QuerySet’ object has no attribute ’id’

數(shù)據(jù)轉(zhuǎn)換

返回響應(yīng)

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) 四、更新一條數(shù)據(jù)

前端需要傳遞pk/id值,通過(guò)pk/id查詢數(shù)據(jù),查詢一條數(shù)據(jù)必須用get,不能用filter,否則會(huì)報(bào)錯(cuò):AttributeError: ’QuerySet’ object has no attribute ’id’

更新一條數(shù)據(jù)時(shí)必須使用filter來(lái)查詢數(shù)據(jù)集,再使用update(**data)來(lái)更新數(shù)據(jù),不能使用get,否則會(huì)報(bào)錯(cuò):AttributeError: ’模型類’ object has no attribute ’update’

get查詢獲取到的是數(shù)據(jù)對(duì)象,而filter查詢獲取到的是數(shù)據(jù)集

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)五、刪除某一條數(shù)據(jù)

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

上述的操作只能適用于數(shù)據(jù)表中字段很少的情況,如果字段較多,寫(xiě)起來(lái)會(huì)很麻煩,不利于開(kāi)發(fā)

總結(jié)

到此這篇關(guān)于Django學(xué)習(xí)筆記之View操作指南的文章就介紹到這了,更多相關(guān)Django View操作內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 亚洲成人影院在线观看 | 交视频在线观看国产 | 日韩视频一区二区 | 欧美日韩电影一区二区 | 国产成人综合一区二区三区 | 波多野结衣三区 | 成人在线视频观看 | 日韩免费av一区二区 | 午夜影视| 中文字幕永久第一页 | 日韩不卡一区二区 | 91久久夜色精品国产网站 | 亚洲成人日韩 | 国产另类ts人妖一区二区 | 国产亚洲欧美一区 | 亚洲午夜精品视频 | 亚洲伊人精品酒店 | 一区二区三区免费在线 | 亚洲成人一区二区 | 免费av手机在线观看 | 91精品久久| 日韩欧美国产一区二区三区 | 欧美国产日韩一区 | 欧美性一区二区三区 | 国产一级特黄aaa | 亚洲一区二区三区四区五区中文 | 妞干网av| 亚洲高清一区二区三区 | 成人高清视频在线观看 | 欧美精品一二三区 | 国产福利在线观看 | 欧美天堂在线观看 | 国产美女精品一区二区三区 | 久久久久久久久久久久网站 | 成人免费视频观看视频 | 免费黄色小视频 | 日韩一级大片 | 99久久久无码国产精品 | 亚洲欧美激情精品一区二区 | 欧美午夜在线观看 | 日韩欧美在线观看视频 |