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操作内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!