import random
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
from TwoApp.models import Person, IDCard, Grade, Student, User, Goods
def hello(request):
return HttpResponse('hello django!')
def index(request):
return render(request, 'index.html')
######################## 一对一 ########################
def addperson(request):
per = Person()
per.p_name = 'atom' + str(random.randint(1,10000))
per.p_age = random.randint(10,50)
per.save()
return HttpResponse('添加 {} 成功'.format(per.p_name))
def addcart(request):
idcart = IDCard()
idcart.i_num = '441622xxxxxxxxxxx{}'.format(random.randint(1000,10000))
idcart.i_addr = '深圳市宝安区西部硅谷-' + str(random.randint(1,10000))
# 身份证 绑定 人
# 绑定到 最后一个人身上
per = Person.objects.last()
idcart.i_person = per # 其实是per.id
idcart.save()
return HttpResponse('身份证{},绑定人:{}'.format(idcart.i_num, per.p_name))
def delperson(request):
per = Person.objects.last()
per.delete()
return HttpResponse('人道毁灭成功!')
def delcart(request):
idcart = IDCard.objects.last()
idcart.delete()
return HttpResponse('销户成功(身份证)!')
def get_person_cart(request):
per = Person.objects.last()
# 获取这个人对应的身份证
# 主获取从, 关系字段是隐性
idcart = per.idcard # 对应的关系表 IDCard --> 小写idcard
return HttpResponse('{}出示身份证: {}'.format(per.p_name, idcart.i_num))
def get_cart_person(request):
cart = IDCard.objects.last()
# 从获取主, 关系字段直接获取
per = cart.i_person # 身份证对应的人
return HttpResponse('身份证:{} 找到的人是: {}'.format(cart.i_num, per.p_name))
######################## 一对多 ########################
def addgrade(request):
grade = Grade()
grade.g_name = 'python18' + str(random.randint(10,99))
grade.save()
return HttpResponse('班级 {} 添加成功! '.format(grade.g_name))
def addstu(request):
stu = Student()
stu.s_name = '王总-' + str(random.randint(1,10000))
stu.s_score = random.randint(1,100)
stu.s_detail = str(random.randint(1,100)) + '我住隔壁,我性王,你有事情我帮忙!'
# 将该学生加到最后一个班级中
grade = Grade.objects.last()
stu.s_grade = grade
stu.save()
return HttpResponse('{}添加 {} 学生成功!'.format(grade.g_name,stu.s_name))
def delgrade(request):
grade = Grade.objects.last()
grade.delete()
return HttpResponse('删除班级成功!')
def delstu(request):
stu = Student.objects.last()
stu.delete()
return HttpResponse('删除学生成功!')
def showstudents(request):
# 班级
grade = Grade.objects.last()
# 主获取从,隐性访问: 对象.关系表(小写)_set.all()
# 该班级对应的学生
# Student.objects.filter(s_grade=grade.id)
# student_set 是objects同源
students = grade.student_set.all()
print(students)
str = '<h1>{} 班级学生信息</h1>'.format(grade.g_name)
for stu in students:
str += '<p> 学号:{} 姓名:{} 成绩: {} </p>'.format(stu.id, stu.s_name, stu.s_score)
return HttpResponse(str)
def showgrade(request):
stu = Student.objects.last()
# 从获取主,显性访问: 对象.属性
grade = stu.s_grade
return HttpResponse(grade.g_name)
######################## 多对多 ########################
def adduser(request):
user = User()
user.u_name = '小黄狗-' + str(random.randint(1,1000))
user.u_tel = random.randint(1000000000, 10000000000)
user.save()
return HttpResponse('{}用户注册成功!'.format(user.u_name))
def addgoods(request):
goods = Goods()
goods.g_name = 'iPhone ' + str(random.randint(1,11))
goods.g_price = random.randint(6000, 10000)
goods.save()
return HttpResponse('{} 添加成功!'.format(goods.g_name))
def add_goods_to_user(request): # 添加购物车
goods = Goods.objects.last()
user = User.objects.last()
# 用户 添加商品 到 购物车
goods.g_user.add(user)
goods.save()
return HttpResponse('{} 收藏商品: {}'.format(user.u_name,goods.g_name))
def show_user_goods(request): # 显示购物车
# 一个用户 对应 多个商品
user = User.objects.last()
goods_list = user.goods_set.all()
# print(goods_list)
str = '<h1> {} 购物车 </h1>'.format(user.u_name)
for goods in goods_list:
str += '<p>{} ${}</p>'.format(goods.g_name,goods.g_price)
return HttpResponse(str)
def add_collect(request): # 添加收藏
goods = Goods.objects.last()
user = User.objects.last()
goods.g_user.add(user)
goods.save()
return HttpResponse('{}商品收藏成功[{}]'.format(goods.g_name,user.u_name))
def show_collect(request): # 显示收藏
# 一个商品 对应 多个用户
goods = Goods.objects.last()
user_list = goods.g_user.all()
return HttpResponse('{}商品收藏数量: {}'.format(goods.g_name, user_list.count()))
网友评论