序列化和反序列化

# Create your views here.
import json

from django.shortcuts import render

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.contrib.auth import authenticate, login
from django.views import View
from app01.models import Projects
from django.db.models import Q


class IndexView(View):

def get(self, request):

# 创建 --
# 方法一:调用save()方法,插入sql语句
# one_obj1 = Projects(name='NB项目11', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')
# one_obj1.save()

# 方法二:
# Projects.objects.create(name='NB项目66', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')

# 查询 --

# 获取数据所有记录
# Projects.objects.all()
# 获取某个记录, get一般获取主键
# one_obj1 = Projects.objects.get(id=2)
# print(one_obj1.name, one_obj1.leader)
# 获取多个记录filter()和exclute()取反, one_obj是查询集
# one_obj = Projects.objects.filter(id=3)
# 模型类属性__contains,【包含】 __icontains忽略大小写 __in获取范围。。。 ____startswith以什么开头。。。。。。
# obj = Projects.objects.filter(name__contains='接口')
# obj = Projects.objects.filter(name__icontains='IBaa')
# obj = Projects.objects.filter(name__startswith='icon')
# obj = Projects.objects.filter(leader__in=['洋', '李白'])

# 关联查询: interface__name
# obj = Projects.objects.filter(interface__name='NB项目22')

# 比较查询:大于__gt, 大于等于:__gte
# obj = Projects.objects.filter(id__gt=2)

# Q 指定多个条件
# obj = Projects.objects.filter(Q(leader='洋') | Q(name__contains='接口测试'))

# 查询姐相当于列表,支持正值切片,索引,for
# 查询集对数据库进行优化,会缓存结果
# 惰性查询
# 链式操作
# obj = Projects.objects.filter(name__contains="接口").filter(developer='洋111')
# obj = Projects.objects.filter(name__contains="NB").first()
# print(obj)

# 修改 --

# obj = Projects.objects.filter(id=1).update(tester='羊羊羊')
# print(obj)

# 删除 --

# obj = Projects.objects.filter(developer__contains='NB').filter(name__contains='33').delete()
# print(obj)

# 排序 name相同时根据-tester排序
# obj = Projects.objects.filter(id__gte=3).order_by('name', '-tester')
# print(obj)

# pass
# return JsonResponse()
# def get(self, request, pk):

# times = datetime.datetime.now()
# return HttpResponse("<h1>ok</h1>")
return HttpResponse([b'<h1>Hello, get!</h1>'])
# return render(request, 'cur_time.html', {'times': times})

def post(self, request):
# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 2. 向数据库新增数据
# new_project = Projects.objects.create(name=python_data['name'], leader=python_data['leader'],
# tester=python_data['tester'], developer=python_data['developer'],
# publish_app=python_data['publish_app'], desc=python_data['desc'])

project = Projects.objects.create(**python_data)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

# return HttpResponse([b'<h1>Hello, post!</h1>'])

def delete(self, request):
return HttpResponse([b'<h1>Hello, delete!</h1>'])

def put(self, request):
return HttpResponse([b'<h1>Hello, put!</h1>'])


class ProjectDetail(View):

def get(self, request, pk):

# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic)

def put(self, request, pk):

# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 获取要修改的数据
project = Projects.objects.get(id=pk)

# 更新数据
project.name = python_data['name']
project.leader = python_data['leader']
project.tester = python_data['tester']
project.developer = python_data['developer']
project.publish_app = python_data['publish_app']
project.desc = python_data['desc']

project.save()

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

def delete(self, request, pk):
# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)
project.delete()

return JsonResponse(None, safe=False, status=204)


# ===============================================================================================
def login(request):
if request.POST:
username = password = ''
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
request.session['user'] = username
response = HttpResponseRedirect('/home/')
return response
else:
return render(request, 'login.html', {'error': 'username or password error'})
# return render(request, 'login.html')

return render(request, 'login.html')


def test(request):
return HttpResponse("hello test !")


def home(request):
return render(request, 'home.html')


def logout(request):
auth.logout(request)
return render(request, 'login.html')

# Create your views here.
import json

from django.shortcuts import render

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.contrib.auth import authenticate, login
from django.views import View
from app01.models import Projects
from django.db.models import Q


class IndexView(View):

def get(self, request):

# 创建 --
# 方法一:调用save()方法,插入sql语句
# one_obj1 = Projects(name='NB项目11', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')
# one_obj1.save()

# 方法二:
# Projects.objects.create(name='NB项目66', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')

# 查询 --

# 获取数据所有记录
# Projects.objects.all()
# 获取某个记录, get一般获取主键
# one_obj1 = Projects.objects.get(id=2)
# print(one_obj1.name, one_obj1.leader)
# 获取多个记录filter()和exclute()取反, one_obj是查询集
# one_obj = Projects.objects.filter(id=3)
# 模型类属性__contains,【包含】 __icontains忽略大小写 __in获取范围。。。 ____startswith以什么开头。。。。。。
# obj = Projects.objects.filter(name__contains='接口')
# obj = Projects.objects.filter(name__icontains='IBaa')
# obj = Projects.objects.filter(name__startswith='icon')
# obj = Projects.objects.filter(leader__in=['洋', '李白'])

# 关联查询: interface__name
# obj = Projects.objects.filter(interface__name='NB项目22')

# 比较查询:大于__gt, 大于等于:__gte
# obj = Projects.objects.filter(id__gt=2)

# Q 指定多个条件
# obj = Projects.objects.filter(Q(leader='洋') | Q(name__contains='接口测试'))

# 查询姐相当于列表,支持正值切片,索引,for
# 查询集对数据库进行优化,会缓存结果
# 惰性查询
# 链式操作
# obj = Projects.objects.filter(name__contains="接口").filter(developer='洋111')
# obj = Projects.objects.filter(name__contains="NB").first()
# print(obj)

# 修改 --

# obj = Projects.objects.filter(id=1).update(tester='羊羊羊')
# print(obj)

# 删除 --

# obj = Projects.objects.filter(developer__contains='NB').filter(name__contains='33').delete()
# print(obj)

# 排序 name相同时根据-tester排序
# obj = Projects.objects.filter(id__gte=3).order_by('name', '-tester')
# print(obj)

# pass
# return JsonResponse()
# def get(self, request, pk):

# times = datetime.datetime.now()
# return HttpResponse("<h1>ok</h1>")
return HttpResponse([b'<h1>Hello, get!</h1>'])
# return render(request, 'cur_time.html', {'times': times})

def post(self, request):
# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 2. 向数据库新增数据
# new_project = Projects.objects.create(name=python_data['name'], leader=python_data['leader'],
# tester=python_data['tester'], developer=python_data['developer'],
# publish_app=python_data['publish_app'], desc=python_data['desc'])

project = Projects.objects.create(**python_data)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

# return HttpResponse([b'<h1>Hello, post!</h1>'])

def delete(self, request):
return HttpResponse([b'<h1>Hello, delete!</h1>'])

def put(self, request):
return HttpResponse([b'<h1>Hello, put!</h1>'])


class ProjectDetail(View):

def get(self, request, pk):

# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic)

def put(self, request, pk):

# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 获取要修改的数据
project = Projects.objects.get(id=pk)

# 更新数据
project.name = python_data['name']
project.leader = python_data['leader']
project.tester = python_data['tester']
project.developer = python_data['developer']
project.publish_app = python_data['publish_app']
project.desc = python_data['desc']

project.save()

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

def delete(self, request, pk):
# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)
project.delete()

return JsonResponse(None, safe=False, status=204)


# ===============================================================================================
def login(request):
if request.POST:
username = password = ''
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
request.session['user'] = username
response = HttpResponseRedirect('/home/')
return response
else:
return render(request, 'login.html', {'error': 'username or password error'})
# return render(request, 'login.html')

return render(request, 'login.html')


def test(request):
return HttpResponse("hello test !")


def home(request):
return render(request, 'home.html')


def logout(request):
auth.logout(request)
return render(request, 'login.html')


# Create your views here.
import json

from django.shortcuts import render

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.contrib.auth import authenticate, login
from django.views import View
from app01.models import Projects
from django.db.models import Q


class IndexView(View):

def get(self, request):

# 创建 --
# 方法一:调用save()方法,插入sql语句
# one_obj1 = Projects(name='NB项目11', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')
# one_obj1.save()

# 方法二:
# Projects.objects.create(name='NB项目66', leader='洋', tester='NB测试', developer='NB开发',
# publish_app='NB应用', desc='太NB了')

# 查询 --

# 获取数据所有记录
# Projects.objects.all()
# 获取某个记录, get一般获取主键
# one_obj1 = Projects.objects.get(id=2)
# print(one_obj1.name, one_obj1.leader)
# 获取多个记录filter()和exclute()取反, one_obj是查询集
# one_obj = Projects.objects.filter(id=3)
# 模型类属性__contains,【包含】 __icontains忽略大小写 __in获取范围。。。 ____startswith以什么开头。。。。。。
# obj = Projects.objects.filter(name__contains='接口')
# obj = Projects.objects.filter(name__icontains='IBaa')
# obj = Projects.objects.filter(name__startswith='icon')
# obj = Projects.objects.filter(leader__in=['洋', '李白'])

# 关联查询: interface__name
# obj = Projects.objects.filter(interface__name='NB项目22')

# 比较查询:大于__gt, 大于等于:__gte
# obj = Projects.objects.filter(id__gt=2)

# Q 指定多个条件
# obj = Projects.objects.filter(Q(leader='洋') | Q(name__contains='接口测试'))

# 查询姐相当于列表,支持正值切片,索引,for
# 查询集对数据库进行优化,会缓存结果
# 惰性查询
# 链式操作
# obj = Projects.objects.filter(name__contains="接口").filter(developer='洋111')
# obj = Projects.objects.filter(name__contains="NB").first()
# print(obj)

# 修改 --

# obj = Projects.objects.filter(id=1).update(tester='羊羊羊')
# print(obj)

# 删除 --

# obj = Projects.objects.filter(developer__contains='NB').filter(name__contains='33').delete()
# print(obj)

# 排序 name相同时根据-tester排序
# obj = Projects.objects.filter(id__gte=3).order_by('name', '-tester')
# print(obj)

# pass
# return JsonResponse()
# def get(self, request, pk):

# times = datetime.datetime.now()
# return HttpResponse("<h1>ok</h1>")
return HttpResponse([b'<h1>Hello, get!</h1>'])
# return render(request, 'cur_time.html', {'times': times})

def post(self, request):
# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 2. 向数据库新增数据
# new_project = Projects.objects.create(name=python_data['name'], leader=python_data['leader'],
# tester=python_data['tester'], developer=python_data['developer'],
# publish_app=python_data['publish_app'], desc=python_data['desc'])

project = Projects.objects.create(**python_data)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

# return HttpResponse([b'<h1>Hello, post!</h1>'])

def delete(self, request):
return HttpResponse([b'<h1>Hello, delete!</h1>'])

def put(self, request):
return HttpResponse([b'<h1>Hello, put!</h1>'])


class ProjectDetail(View):

def get(self, request, pk):

# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic)

def put(self, request, pk):

# 1.前端数据转化成python要求数据
json_data = request.body.decode('utf-8')
python_data = json.loads(json_data, encoding='utf-8')

# 获取要修改的数据
project = Projects.objects.get(id=pk)

# 更新数据
project.name = python_data['name']
project.leader = python_data['leader']
project.tester = python_data['tester']
project.developer = python_data['developer']
project.publish_app = python_data['publish_app']
project.desc = python_data['desc']

project.save()

# 3. 将模型类转化为字典,然后返回
one_dic = {
'name': project.name,
'leader': project.leader,
'tester': project.tester,
'developer': project.developer,
'publish_app': project.publish_app,
'desc': project.desc,
}

return JsonResponse(one_dic, status=201)

def delete(self, request, pk):
# 1. 校验ID值PK,是否为正整数,在数据库中是否存在
# 2. 获取指定pk值的项目
project = Projects.objects.get(id=pk)
project.delete()

return JsonResponse(None, safe=False, status=204)


# ===============================================================================================
def login(request):
if request.POST:
username = password = ''
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
request.session['user'] = username
response = HttpResponseRedirect('/home/')
return response
else:
return render(request, 'login.html', {'error': 'username or password error'})
# return render(request, 'login.html')

return render(request, 'login.html')


def test(request):
return HttpResponse("hello test !")


def home(request):
return render(request, 'home.html')


def logout(request):
auth.logout(request)
return render(request, 'login.html')

序列化和反序列化


上一篇:android sensor架构


下一篇:程序员也是等级分明的,你属于哪个等级呢