拿去当期末作业
一、运行效果
二、说明
本实例是基于Python3+Django+Sqlite实现的,Django入门级项目(主要是练习数据库的CURD),对于想要练习Django的同学来说,通过这个实例能够加深对数据库基本操作的理解
运行流程
解压
切换安装有Django的Python虚拟环境
运行命令python3 manage.py runserver
三、部分示例代码
# -*- coding:utf-8 -*-from django.shortcuts import render,redirectfrom django.views.decorators.csrf import csrf_exemptfrom django.conf import settings # 获取 settings.py 里边配置的信息import osfrom .models import *# 1.1.前往 index 页(all)defall_page(request): data = student_info.objects.all() content={'data': data}return render(request, 'student/all.html', content)# 1.2.前往 add 页defadd_page( request ):return render(request, 'student/add.html')# 2.增@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 带你进群一起学编程