配置urls.py
格式:url('目標(biāo)地址', views.方法)
(給我的感覺像是springboot中的@RequetMapping)
數(shù)據(jù)庫(kù)操作方法 --views
打開views.py,在這里寫在urls.py中配置時(shí)寫的views.方法中的方法
create_stu
# 創(chuàng)建學(xué)生信息
# 引入ORM概念:對(duì)象關(guān)系映射
# 第一種
# Student2.objects.create(s_name='Mark')
# 第二種
# stu = Student2()
# stu.s_name = 'joe'
# stu.save()
# 第三種
# 初始化
stu = Student2()
stu.save()
return HttpResponse('創(chuàng)建學(xué)生方法')
select_stu
# 查詢數(shù)據(jù)
"""
"""
# select * from app_student
stus = Student2.objects.all()
# 獲取學(xué)生的姓名
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)
# 查詢年紀(jì)為19的學(xué)生
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或者年紀(jì)=19的學(xué)生
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('修改')