配置urls.py
格式:url('目标地址', views.方法)
(给我的感觉像是springboot中的@RequetMapping)
数据库操作方法 --views
打开views.py,在这里写在urls.py中配置时写的views.方法中的方法
create_stu
# 创建学生信息
# 引入ORM概念:对象关系映射
# 第一种
# Student2.objects.create(s_name='Mark')
# 第二种
# stu = Student2()
# stu.s_name = 'joe'
# stu.save()
# 第三种
# 初始化
stu = Student2()
stu.save()
return HttpResponse('创建学生方法')
select_stu
# 查询数据
"""
"""
# select * from app_student
stus = Student2.objects.all()
# 获取学生的姓名
for stu in stus:
print(stu.s_name)
# select * from xxx where s_name=''
stu_m = Student2.objects.filter(s_name='mark')
print(stu_m)
# 查询年纪为19的学生
stu_s = Student2.objects.filter(s_age=19)
stu_names = [stu.s_name for stu in stu_s]
for name in stu_names:
print(name)
# 姓名不等于xx
stu_e = Student2.objects.exclude(s_name='john')
# 排序,按照id升序/降序===>asc/desc
stus = Student2.objects.all().order_by('-id')
# stus = Student2.objects.all().order_by('-id')---降序
stu_info = [(stu.s_name, stu.id) for stu in stus]
for info in stu_info:
print(info)
# values()
stus = Student2.objects.all().values('id', 's_name')
# get(),first()
stus = Student2.objects.get(id=1)
stus = Student2.objects.filter(id=1).first() #更加安全
stus = Student2.objects.filter(s_name__contains='e').values('id', 's_name')
stus = Student2.objects.filter(s_name__endswith='e').values('id', 's_name')
stus = Student2.objects.filter(s_name__startswith='M').values('id', 's_name')
# Q(),查询姓名叫joe或者年纪=19的学生
stus = Student2.objects.filter(Q(s_name='joe') | Q(s_age=18)).values('id', 's_name')
# ~ 表示取非
return HttpResponse(stus)
delete_stu
# stu = Student2.objects.get(pk=1)
# stu.delete()
# Student2.objects.get(pk=2).first().delete()
return HttpResponse('删除')
update_stu
# 更新
# stu = Student2.objects.get(pk=2)
# stu.s_name = '帅逼'
# stu.save()
# 第二种
Student2.objects.filter(id=3).update(s_age=17)
return HttpResponse('修改')