欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django學習筆記之View操作指南

 更新時間:2021年03月20日 12:24:14   作者:三省吾身  
這篇文章主要給大家介紹了關于Django學習筆記之View操作指南的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Django的View

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

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

導入: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ù)轉成字典

使用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轉字典 
  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 ) 
   # 返回響應 
   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ù)轉換

返回響應

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ā)

總結

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

相關文章

  • 教你用Python讀取CSV文件的5種方式

    教你用Python讀取CSV文件的5種方式

    一個股票的數(shù)據(jù)集,其實就是常見的表格數(shù)據(jù),有自己的頭部和身體,這篇文章主要介紹了用Python讀取CSV文件的5種方式,通過五招給大家介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • python創(chuàng)建學生管理系統(tǒng)

    python創(chuàng)建學生管理系統(tǒng)

    這篇文章主要為大家詳細介紹了python創(chuàng)建學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python爬取個人微信朋友信息操作示例

    Python爬取個人微信朋友信息操作示例

    這篇文章主要介紹了Python爬取個人微信朋友信息操作,涉及Python使用itchat包實現(xiàn)微信朋友信息爬取操作相關實現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • 基于torch.where和布爾索引的速度比較

    基于torch.where和布爾索引的速度比較

    今天小編就為大家分享一篇基于torch.where和布爾索引的速度比較,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python元組操作實例解析

    python元組操作實例解析

    這篇文章主要介紹了python元組操作,以實例的形式較為直觀的講述了Python中元組的特點與用法,需要的朋友可以參考下
    2014-09-09
  • python爬蟲入門教程--利用requests構建知乎API(三)

    python爬蟲入門教程--利用requests構建知乎API(三)

    這篇文章主要給大家介紹了關于python爬蟲入門之利用requests構建知乎API的相關資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • Python continue語句實例用法

    Python continue語句實例用法

    在本篇文章里小編給大家整理了關于Python continue語句實例用法,有需要的朋友們可以跟著學習下。
    2020-02-02
  • 用Python寫漏洞驗證腳本的代碼

    用Python寫漏洞驗證腳本的代碼

    這篇文章主要介紹了用Python寫漏洞驗證腳本,本文給大家分享完整實例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • 淺析python連接數(shù)據(jù)庫的重要事項

    淺析python連接數(shù)據(jù)庫的重要事項

    這篇文章主要介紹了python連接數(shù)據(jù)庫的重要事項,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 每天遷移MySQL歷史數(shù)據(jù)到歷史庫Python腳本

    每天遷移MySQL歷史數(shù)據(jù)到歷史庫Python腳本

    這篇文章主要為大家詳細介紹了Python實現(xiàn)每天遷移MySQL歷史數(shù)據(jù)到歷史庫的腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論