1、环境
1> CentOS Linux release 7.5.1804 (Core)
2>django 1.11.8
3>mysql Ver 15.1 Distrib 5.5.56-MariaDB
4>apache Apache/2.4.6 (CentOS)
5>python 2.7.5
2、背景
由于工作需求,需要一个练习单选、多选、判断的考试系统,于是简单制作了一个。
3、步骤
1> 安装相关软件
yum -y install epel-release #安装epel包
版本信息
httpd.x86_64 0:2.4.6-80.el7.centos
httpd-devel.x86_64 0:2.4.6-80.el7.centos
mariadb-server.x86_64 1:5.5.56-2.el7
mod_wsgi.x86_64 0:3.4-12.el7_0
python-devel.x86_64 0:2.7.5-68.el7
python2-pip.noarch 0:8.1.2-10.el7
yum -y install httpd-devel python-devel httpd mariadb-server mod_wsgi python-pip #安装相关包
版本信息
django-1.11.8 pymysql-0.9.3 xlrd-1.2.0
安装django、pymysql、xlrd相关模块
2>创建简单项目测试环境
yum -y install httpd mariadb-server #安装apache和MySQL
systemctl start httpd mariadb #启动apache和MySQL
systemctl stop firewalld #关闭防火墙
setenforce 0 #关闭selinux
切换到/var/www/html 创建项目
django-admin startproject ch #创建ch项目
切换到项目目录,并测试
python manage.py runserver #运行项目,并在浏览器测试
如下图:
修改ch目录内的url文件,并创建views.py文件
如urls.py下图:
新建views.py文件,下图:
浏览器测试:
接下来测试apache环境
systemctl start httpd #启动apache,并在浏览器测试
修改apache配置文件
加载mod_wsgi模块(必须)
添加日志,方便查看错误(非必需)
配置文件末尾加入以上内容,其中ch是我的项目名称
192.168.80.147是我虚拟机的IP
浏览器测试报错,是配置文件settings.py文件的问题
debug改为false,allowed_hosts改为‘*’
浏览器测试,没有问题,接下来专门写程序就行了
3>考试系统编写
1、模型创建(models.py)
#coding=utf-8
#-*- coding:utf-8 -*-
from django.db import models
# Create your models here.
class Question(models.Model):
ANSWER=(
('A','A'),
('B','B'),
('C','C'),
('D','D'),
)
id = models.AutoField(primary_key=True)
subject = models.CharField('科目', max_length=20,default='数据中心规划与实施')
title = models.TextField('题目')
optionA=models.CharField('A选项',max_length=300)
optionB=models.CharField('B选项',max_length=300)
optionC=models.CharField('C选项',max_length=300,null=True)
optionD=models.CharField('D选项',max_length=300,null=True)
answer=models.CharField('答案',max_length=10,choices=ANSWER)
score=models.IntegerField('分数',default=1)
classmodel=models.CharField('类型',max_length=30)
unit=models.CharField('单元',max_length=300)
class Meta:
db_table='question'
verbose_name='理论题库'
verbose_name_plural=verbose_name
def __str__(self):
return '<%s:%s>'%(self.subject,self.title);
class Student(models.Model):
sid = models.CharField(max_length=30)
sname = models.CharField(max_length=30)
class Meta:
db_table='student'
verbose_name='学生'
verbose_name_plural=verbose_name
def __str__(self):
return '<%s:%s>'%(self.sid,self.sname);
class ErrorQuestion(models.Model):
ANSWER=(
('A','A'),
('B','B'),
('C','C'),
('D','D'),
)
id = models.AutoField(primary_key=True)
student=models.ForeignKey(Student,on_delete=models.CASCADE,default='')
subject = models.CharField('科目', max_length=20,default='数据中心规划与实施')
title = models.TextField('题目')
optionA=models.CharField('A选项',max_length=300)
optionB=models.CharField('B选项',max_length=300)
optionC=models.CharField('C选项',max_length=300,null=True)
optionD=models.CharField('D选项',max_length=300,null=True)
answer=models.CharField('答案',max_length=10,choices=ANSWER)
myanswer=models.CharField('我的答案',max_length=10)
unit=models.CharField('单元',max_length=300)
class Meta:
db_table='errorquestion'
verbose_name='错题本'
verbose_name_plural=verbose_name
def __str__(self):
return '<%s:%s>'%(self.subject,self.title);
2、注册文件(admin.py)
#coding=utf-8
#-*- coding:utf-8 -*-
from django.contrib import admin
from ch.models import ErrorQuestion,Question,Student
# Register your models here.
# 修改名称
admin.site.site_header='理论刷题系统后台'
admin.site.site_title='理论刷题系统'
@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
list_display = ('id','subject','title','optionA','optionB','optionC','optionD','answer','score')
@admin.register(ErrorQuestion)
class ErrorQuestionAdmin(admin.ModelAdmin):
list_display = ('id','subject','title','optionA','optionB','optionC','optionD','answer','myanswer')
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = ('sid','sname')
3、初始化文件(__init__.py)
import pymysql
pymysql.install_as_MySQLdb()
4、视图函数文件(views.py)
#coding=utf-8
#-*- coding:utf-8 -*-
from django.shortcuts import render,redirect
from django.http import HttpResponse
from . import models
from .islogin import islogin
# Create your views here.
def index(request):
if request.method=='POST':
stu_id = request.POST.get('stu_id')
print('10行',stu_id)
sid = models.Student.objects.get(sid=stu_id)
if sid:
red = render(request,'index.html',{'sid':sid})
# 记住用户名
red.set_cookie('uname', stu_id)
request.session['stu_id'] = sid.id
print('sid.id',sid.id)
request.session['stu_name'] = sid.sname
return red
# return render(request,'index.html',{'sid':sid})
else:
return render(request,'login.html',{'msg':'请输入正确的学号'})
return render(request,'login.html')
@islogin
def startExam(request):
sname = request.session.get('stu_name')
unit = request.GET['unit']
paper=models.Question.objects.filter(unit=unit)
return render(request,'exam.html',{'paper':paper,'subject':unit,'m':len(paper),'sname':sname})
@islogin
def calGrade(request):
if request.method=='POST':
subject1 = request.POST.get('subject')
sname = request.session.get('stu_name')
sid = request.session.get('stu_id')
stu_id = models.Student.objects.get(id=sid).sid
models.ErrorQuestion.objects.filter(student_id=sid).delete()
# # 计算该门考试的学生成绩
question= models.Question.objects.filter(unit=subject1).values()
sub = 0
mygrade=0#初始化一个成绩为0
for p in question:
qId=str(p['id'])#int 转 string,通过pid找到题号
myans=request.POST.getlist(qId)#通过 qid 得到学生关于该题的作答
er = ''
# print(myans)
for i in myans:
er += i
okans=p['answer']#得到正确答案
# print(okans)
if er==okans:#判断学生作答与正确答案是否一致
mygrade+=p['score']#若一致,得到该题的分数,累加mygrade变量
elif er == '':
pass
else:
sub += 1
if p['optionC']:
# #向errorquestion表中插入数据
models.ErrorQuestion.objects.create(subject=subject1,student_id=sid,title=p['title'],optionA=p['optionA'],optionB=p['optionB'],optionC=p['optionC'],optionD=p['optionD'],answer=p['answer'],myanswer=er,unit=p['unit'])
else:
models.ErrorQuestion.objects.create(subject=subject1,student_id=sid,title=p['title'],optionA=p['optionA'],optionB=p['optionB'],answer=p['answer'],myanswer=er,unit=p['unit'])
paper=models.ErrorQuestion.objects.filter(unit=subject1,student_id=sid)
return render(request,'errorq.html',{'paper':paper,'subject':subject1,'mygrade':mygrade,'sub':sub,'sname':sname,'stu_id':stu_id})
5、路由文件(urls.py)
#coding=utf-8
#-*- coding:utf-8 -*-
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.index),
url(r'^startExam',views.startExam),
url(r'^calGrade',views.calGrade),
]
6、装饰器文件(islogin.py)
#coding=utf-8
from django.http import HttpResponseRedirect
#如果登录则转到登录页面
def islogin(func):
def login_fun(request,*args,**kwargs):
if request.session.get('stu_id'):
return func(request,*args,**kwargs)
else:
red = HttpResponseRedirect('/')
red.set_cookie('url',request.get_full_path)
return red
return login_fun
7、将表格中的文件导入到数据库(extransfer.py),共计19单元,表格内容如下:
#coding=utf-8
#-*- coding:utf-8 -*-
import pymysql
import xlrd
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 创建连接
conn = pymysql.connect( # 打开数据库连接
host='127.0.0.1', # IP
port=3306, # 端口
user='root', # 数据库用户名
password="123456",# 密码
database='ch', # 要连那个库
charset='utf8') # 设置字符集
# 拿到游标
cur = conn.cursor() # 默认返回的是元祖 conn.cursor(pymysql.cursors.DictCursor) 返回字典
cur.execute('use bwll;') # 进入数据库
for i in range(1,20):
# 打开Excel文件
bok = xlrd.open_workbook('../static/examunit/第{}单元.xlsx'.format(i))
sht=bok.sheets()[0]
for m in range(1,100):
try:
row1 = sht.row_values(m)
except:
print('break',m)
break
else:
row2= row1[2].replace('(网工)','')
row3= row2.replace('(数据中心)','')
row= row3.replace('()','')
if row1[1][0] == '1':
n = "insert into question(subject,title,optionA,optionB,optionC,optionD,answer,score,classmodel,unit) values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format('数据中心规划与实施',row,row1[3],row1[4],row1[5],row1[6],row1[9],'3','单选题','第{}单元'.format(i))
elif row1[1][0] == '2':
n = "insert into question(subject,title,optionA,optionB,optionC,optionD,answer,score,classmodel,unit) values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format('数据中心规划与实施',row,row1[3],row1[4],row1[5],row1[6],row1[9],'2','多选题','第{}单元'.format(i))
else:
n = "insert into question(subject,title,optionA,optionB,answer,score,classmodel,unit) values('{}','{}','{}','{}','{}','{}','{}','{}');".format('数据中心规划与实施',row,'正确','错误',row1[9],'2','判断题','第{}单元'.format(i))
# print(m)
cur.execute(n)
conn.commit()
cur.execute("select * from question;")
# content = cur.fetchmany(2) # 返回2条数据 (('information_schema',), ('db1',))
content = cur.fetchall() # 返回所有数据
for i in content:
print(i)
cur.close() # 关闭游标
conn.close() # 关闭数据库连接
接下来是templates文件
errorq.html #显示错题的页面
网工理论刷题系统
欢迎{{sname}}登录
exam.html #考试页面
网工理论刷题系统
欢迎{{sname}}登录
index.html #首页,显示考试单元
网工理论刷题系统
login.html #登陆页面
网工理论刷题系统登录
成功了
这一个是整体的,稍后有时间我会上一个在阿里服务器部署的相关文章
登陆账号 4100105207 网址:http://47.95.212.226/
未完待续----------------------------------------------------------------------------------需要上班