濮阳杆衣贸易有限公司

主頁 > 知識庫 > Django學(xué)習筆記之View操作指南

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

熱門標簽:西藏快速地圖標注地點 如何在地圖標注文字 廈門crm外呼系統(tǒng)如何 n400電話申請多少錢 女王谷地圖標注 ai地圖標注 長春人工智能電銷機器人官網(wǎng) 百應(yīng)ai電銷機器人鄭州 地圖標注推廣單頁

Django的View

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

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

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

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

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

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

在JsonResponse必須添加safe=False參數(shù),否則會報錯: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) 

二、創(chuàng)建數(shù)據(jù)

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

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

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" 
  # 校驗數(shù)據(jù) 
  result = UserProfile.objects.filter(Q(code=code) | Q(open_id=open_id)) 
  if not result.exists(): 
   # 數(shù)據(jù)入庫 
   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ù)(單個)

前端需要傳遞pk/id值,通過pk/id查詢數(shù)據(jù),查詢一條數(shù)據(jù)必須用get,不能用filter,否則會報錯: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值,通過pk/id查詢數(shù)據(jù),查詢一條數(shù)據(jù)必須用get,不能用filter,否則會報錯:AttributeError: 'QuerySet' object has no attribute 'id'

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

get查詢獲取到的是數(shù)據(jù)對象,而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ù)表中字段很少的情況,如果字段較多,寫起來會很麻煩,不利于開發(fā)

總結(jié)

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

您可能感興趣的文章:
  • Golang 并發(fā)以及通道的使用方式
  • golang 如何替換掉字符串里面的換行符\n
  • spring boot集成mongodb的增刪改查的示例代碼
  • go并發(fā)實現(xiàn)素數(shù)篩的代碼
  • golang 中的 nil的場景分析
  • 完美解決go Fscanf 在讀取文件時出現(xiàn)的問題

標簽:綿陽 內(nèi)江 興安盟 廊坊 黔東 亳州 渭南 拉薩

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Django學(xué)習筆記之View操作指南》,本文關(guān)鍵詞  Django,學(xué)習,筆記,之,View,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Django學(xué)習筆記之View操作指南》相關(guān)的同類信息!
  • 本頁收集關(guān)于Django學(xué)習筆記之View操作指南的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    拉萨市| 松滋市| 靖州| 云浮市| 监利县| 卫辉市| 手机| 治多县| 满洲里市| 沐川县| 灵宝市| 罗源县| 莎车县| 泽普县| 三都| 遂川县| 宜都市| 广元市| 易门县| 孝义市| 安乡县| 陆良县| 和平区| 沂源县| 礼泉县| 泗洪县| 陈巴尔虎旗| 金秀| 洛扎县| 南宫市| 茌平县| 江安县| 托克逊县| 堆龙德庆县| 抚顺市| 横山县| 五指山市| 丹东市| 麦盖提县| 临邑县| 博白县|