拿去当期末作业
一、运行效果
二、说明
本实例是基于Python3+Django+Sqlite实现的,Django入门级项目(主要是练习数据库的CURD),对于想要练习Django的同学来说,通过这个实例能够加深对数据库基本操作的理解
运行流程
解压
切换安装有Django的Python虚拟环境
运行命令python3 manage.py runserver
三、部分示例代码
from django.shortcuts import render,redirectfrom django.views.decorators.csrf import csrf_exempt
from django.conf import settings import osfrom .models import *
defall_page(request):
data = student_info.objects.all() content={'data': data}return render(request, 'student/all.html', content)
defadd_page( request ):return render(request, 'student/add.html')
@csrf_exemptdefadd_student(request): t_name = request.POST['tName'] t_age = request.POST['tAge'] t_image = request.FILES['tImage'] fname = os.path.join(settings.MEDIA_ROOT, t_image.name)with open(fname, 'wb') as pic:for c in t_image.chunks(): pic.write(c)
student=student_info() student.t_name=t_name student.t_age=t_age student.t_image = os.path.join("/static/media/", t_image.name) student.save()
return redirect('/allPage')
来源:https://www.itprojects.cn/162.html
不忘编程初心,一起学习!
加微信 python1068 带你进群一起学编程