import random
from django.db.models import Max, Avg, F, Q
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
# 视图函数
# 第一个参数request
from app.models import Student
def hello(request):
return HttpResponse('hello django!')
def home(request):
# render 其实调用的 HttpResponse
return render(request, 'home.html')
# 添加学生
def addStudent(request):
# 学生对象
stu = Student()
stu.s_name = '张' + str(random.randint(1,10000))
stu.s_age = random.randint(10,50)
stu.s_sex = '男'
stu.s_score = random.randint(0,101)
# 写入
stu.save()
return HttpResponse('添加学生成功: ' + stu.s_name)
# 获取所有学生
def getStudents(request):
# all() 所有数据
students = Student.objects.all()
# filter() 过滤[按条件查询where]
# filter(键=值)
# students = Student.objects.filter(id=3)
# 成绩 > 50
# students = Student.objects.filter(s_score__gt=50)
# 多个条件
# filter(键=值,键=值...)
# students = Student.objects.filter(s_score__gt=50).filter(s_age__lt=40)
# pk primary_key
# id 默认主键 【pk和id是等价的】
# students = Student.objects.exclude(pk__gt=10)
# 排序
# students = Student.objects.order_by('-s_score')
# students = Student.objects.all()
# print(students)
# students = Student.objects.values()
# print(students)
# 包含
# students = Student.objects.filter(s_name__contains='3')
# 以xx开头
# students = Student.objects.filter(s_name__startswith='王')
# students = Student.objects.filter(s_name__endswith='0')
# in
# students = Student.objects.filter( pk__in=[1,3,5,7,9] )
# 时间
# students = Student.objects.filter( s_date__year=2018 )
str = ''
for student in students:
str += '<p> 学号:%d 姓名:%s 年龄: %d 成绩:%d </p>' % (student.id, student.s_name,student.s_age, student.s_score)
return HttpResponse(str)
def getone(request):
# stu = Student.objects.get(pk=3)
# stu = Student.objects.get(s_score=0)
# stu = Student.objects.get(s_score=-100)
# students = Student.objects.filter(pk=1)
students = Student.objects.filter(s_score=-100)
# if students.count()>0: # 有数据
# stu = students.first()
# else: # 无数据
# stu = '无数据'
if students.exists():
stu = students.first()
else: # 无数据
stu = '无数据'
return HttpResponse(stu)
def pagestu(request):
# Student.objects.all()[x:y] ==> (x.y]
students = Student.objects.all()[1:3]
return HttpResponse(students)
def agg(request):
m_score = Student.objects.aggregate( Max('s_score') )
print(m_score['s_score__max'])
m_avg = Student.objects.aggregate( Avg('s_score') )
print(m_avg)
return HttpResponse('聚合函数')
def fobj(request):
# 年龄 大于 分数
students = Student.objects.filter(s_age__gt=F('s_score'))
return HttpResponse(students)
def qobj(request):
# and 操作
# students = Student.objects.filter(s_age__gt=18).filter(s_score__gt=80)
# students = Student.objects.filter( Q(s_age__gt=18) & Q(s_score__gt=80) )
# students = Student.objects.filter( Q(pk=3) | Q(pk=5) | Q(pk=10) )
students = Student.objects.filter( ~Q(pk__lt=5) )
return HttpResponse(students)
def update(request):
stu = Student.objects.get(pk=2)
stu.s_name = '张二'
stu.save()
return HttpResponse('更新数据')
def delstu(request):
stu = Student.objects.get(pk=4)
stu.delete()
return HttpResponse('删除数据')
网友评论