python结课大作业
首先代码全是根据老师给的框架,自己写的,而且运行过后也没有问题,如果遇到问题的小盆友们私信我~
(一)django安装
(1)django是什么
django是基于python的开源web开发框架。发音:jang-oh。(d不发音)
(2)基于django的网站
(3)django的设计理念
使Web开发人员更便捷地开发网站
采用MTV设计模式
-M - Model (模型)
-T - Template (模板)
-V - View (视图)
(4)django安装
a.下载安装包
(其实你们自己在官网或者某些平台上也可下载,这个我也不知道是不是最新的了😓)
b.解压
c.打开cmd命令行,cd到解压目录。
d.运行
python setup.py install
(这里默认大家pycharm环境已经配置完成,在输入这条指令后django就安装完成了)
(二)数据库安装
u1s1应该先聊数据库再聊django。不过问题不大,在这个实验中我们只是把数据库当成我们存放数据的工具,并没用用到sql语句。
(emm战神的数据库我们可能下下个学期会开。。这部分多数来自老师的课件)
(1)数据库(DataBase)
有组织的数据集
(2)数据库管理系统(DataBase Management System, DBMS)
一套计算机软件,提供用户和数据库之间的接口
功能:
数据定义
数据更新(增删改)
数据查询
数据库管理
(3)常见的数据库管理系统
(4)SQLite3安装
a.在 Windows 系统上安装
http://sqlite.org/snapshot/sqlite-dll-win64-x64-201408081749.zip或http://sqlite.org/snapshot/sqlite-dll-win32-x86-201408081749.zip
b.其他系统的安装
建议如果大家没装过虚拟机可以找个教程装一下。推荐vmware的ubuntu或centos。老师推荐的是VirtualBox,问题会少一些。提前熟悉一下Ubuntu的操作,不管是cmd还是terminal都大有裨益。
c.SQLite3命令行
创建或者打开一个数据库(db是数据库,前面是数据库的名字)
sqlite3 nmsl.db
创建一个表
sqlite> create table student(id, name, room, phone);
插入一行
sqlite> insert into student values(150310101, 'Zhang', 'B1502', 86411234);
(三)学生管理系统
(1)类
基于python的django开发,文件目录大概就像上面这张图一样。当然因为当时这个工程ddl和期末考试时间差不多,所以做的功能是最基本的,读者可以在其上进行扩展。
models
models.py文件写的是数据库对应的类。比如我写的Student1类,参数为models.Model,类中的变量有姓名,性别,数语外理化生,总分等等。字符串型的就用models.CharField,布尔型的用models.BooleanField(比如性别,可以设定为男人为1,女人为0,是不是很形象),成绩可以用int或者float都可以,这里使用int型。
app.py
from django.apps import AppConfig
class AddrBookConfig(AppConfig):
name = 'addr_book'
startproject
> django-admin.py startproject mysite
runserver
记住这个runserver,我们如果想在pycharm中运行网页,每次在命令行里都要输入该命令。
> python manage.py runserver
startapp
> python manage.py startapp addr_book
在settings.py中,如图位置,修改添加为‘addr_book’
views.py
编辑views.py文件
views.py是python文件,我们需要在该文件中编写需要用到的函数,比如,增删改查,排序等等。每个函数的参数都是request,函数运行的条件是request.POST不为0。
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from addr_book.models import Student1
from django.views.decorators.csrf import csrf_exempt
import datetime
@csrf_exempt
def insert(request):
if request.POST:
post = request.POST
temp = (int)(post["math"])+(int)(post["chinese"])+(int)(post["english"])+(int)(post["physics"])+(int)(post["chemistry"])+(int)(post["biology"])
new_people = Student1(
name = post["name"],
math = post["math"],
chinese = post["chinese"],
english = post["english"],
physics = post["physics"],
chemistry = post["chemistry"],
biology = post["biology"],
sum = temp)
if post["sex"] == 'M':
new_people.sex = True
else:
new_people.sex = False
new_people.save()
return render(request, 'insert.html')
def list(request):
student_list = Student1.objects.all()
c = {
"student_list":student_list,}
return render(request, "list.html", c)
def menu(request):
return render(request,'menu.html')
def search(request):
if request.POST:
post = request.POST
name1=post["name"]
student_list = Student1.objects.all()
c = {
"student_list": student_list, }
return render(request, "search.html", locals())
def consearch(request):
if request.POST:
post=request.POST
student_list = Student1.objects.all()
stu_lst=[]
if(post["subject"]=="sum"):
for student in student_list:
if student.sum>=int(post["min"]) and student.sum<=int(post["max"]):
stu_lst.append(student)
stu_lst.sort(key=lambda x:x.sum,reverse=True)
elif(post["subject"]=="math"):
for student in student_list:
if student.math>=int(post["min"]) and student.math<=int(post["max"]):
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.math, reverse=True)
elif (post["subject"] == "chinese"):
for student in student_list:
if student.chinese >= post["min"] and student.math <= post["max"]:
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.chinese, reverse=True)
elif (post["subject"] == "english"):
for student in student_list:
if student.english >= post["min"] and student.math <= post["max"]:
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.english, reverse=True)
elif (post["subject"] == "physics"):
for student in student_list:
if student.physics >= post["min"] and student.math <= post["max"]:
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.physics, reverse=True)
elif (post["subject"] == "chemistry"):
for student in student_list:
if student.chemistry >= post["min"] and student.math <= post["max"]:
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.chemistry, reverse=True)
elif (post["subject"] == "biology"):
for student in student_list:
if student.biology >= post["min"] and student.math <= post["max"]:
stu_lst.append(student)
stu_lst.sort(key=lambda x: x.biology, reverse=True)
return render(request, 'consearch.html',locals())
def sort(request):
if request.POST:
post=request.POST
stu_list