Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统
1 准备工作
1.1 环境搭建
1.1.1 安装python3.6
1.1.2 安装django2.2
pip install django(==2.2.0) //不加版本默认安装最新版
1.1.3 安装pycharm(社区版,官网下载安装即可)
在安装完成后要配置好需要的第三方库:(pip下载,推荐在pycharm下也配置虚拟环境)
django2.2
连接mysql需要的库:pymysql, mysql, mysqlclinet
验证码用到的库:django-simple-captcha
(只需在虚拟环境下配置)
(由于下载库较多,忘记用到的库,下附截图)
1.1.4 安装数据库,
我使用的是mysql,推荐安装界面管理文件(我使用的是mysqlworkbench)数据库配置,settings.py文件
databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name':'cet', #数据库名字 'user':'root', #登陆数据库的用户名 'password':'123', #登陆数据库的密码 'host':'localhost', #数据库的ip地址 'port':'3306', #ip地址的端口号,默认(3306) } }
__init__.py里面导入pymysql
import pymysql pymysql.install_as_mysqldb()
1.2 创建django项目及app
1.2.1 创建指令
django-admin startproject project_name #创建项目 python manage.py startapp app_name #创建app(可能会报错) #上面创建app失败用下面这个指令 django-admin startapp app_name 1.2.2 注册app installed_apps = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'login', #登录注册 'captcha', #验证码 'home', #报名主页 ]
1.4 更改时区和语言
settings.py文件中,将默认改为亚洲/上海和中文
language_code = 'zh-hans' time_zone = 'asia/shanghai' use_i18n = true use_l10n = true use_tz = false
1.5 数据库迁移
更改models.py后,要将更改同步到数据库之中,这时就要用到数据库迁移指令(若迁移失败:no changes问题)。
python manage.py makemigrations python manage.py migrate
1.6 创建超级管理员
命令行冲使用指令创建管理员账号用于使用django自带的框架。
python manage.py createsuperuser
1.7 运行准备
添加端口号:
2 注册登录模块
(我将其放在了app名为login里面)
2.1 数据库模型设计
特殊参数说明:verbose_name——用于修改django框架各表成员的名字(相当于副名,只用于显示),其他可以从文章
开头推荐的博客了解。
#login/models.py from django.db import models # create your models here. class user(models.model): '''用户表''' gender = ( ('male', '男'), ('female', '女'), ) name = models.charfield(verbose_name="用户名", max_length=128, unique=true) # unique表示唯一 password = models.charfield(verbose_name="密码", max_length=256) email = models.emailfield(verbose_name="邮箱", unique=true) sex = models.charfield(verbose_name="性别", max_length=32, choices=gender, default='男') c_time = models.datetimefield(auto_now_add=true) def __str__(self): return self.name #用于将django自带管理员端汉化 class meta: ordering = ['c_time'] verbose_name = '用户' verbose_name_plural = '用户'
2.2 在admin中注册模型
#login/admin.py from django.contrib import admin # register your models here. from . import models admin.site.register(models.user)
2.3 创建表单
在我们创建数据库用户表后,采用表单验证前端的数据并进行存储到数据库中。
#login/forms.py from django import forms from captcha.fields import captchafield class user_entry(forms.form): user_name = forms.charfield(label="用户名", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) user_true_name = forms.charfield(label="真实姓名", max_length=128,widget=forms.textinput(attrs={'class': 'form-control'})) user_id = forms.charfield(label="身份证号", max_length=18, widget=forms.textinput(attrs={'class': 'form-control'})) email = forms.emailfield(label="邮箱地址", widget=forms.emailinput(attrs={'class': 'form-control'})) exam_point = forms.charfield(label="考点", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) class user_datas(forms.form): email = forms.emailfield(label="邮箱地址", widget=forms.emailinput(attrs={'class': 'form-control'})) user_name = forms.charfield(label="用户名", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) user_true_name = forms.charfield(label="真实姓名", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) user_id = forms.charfield(label="身份证号", max_length=18, widget=forms.textinput(attrs={'class': 'form-control'})) # user_img = user_school = forms.charfield(label="所在学校", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) # user_f_score = forms.charfield(label="四级成绩", max_length=10, widget=forms.textinput(attrs={'class': 'form-control'})) # user_s_score = forms.charfield(label="六级成绩", max_length=10, widget=forms.textinput(attrs={'class': 'form-control'}))
2.4 路由及视图设计
2.4.1 路由设计
由于登陆注册使用的路径不多,就将其全放在项目目录下的urls.py文件,当然也可以采用项目和app相结合。
#项目名/urls.py from django.contrib import admin from django.urls import path,include from login import views urlpatterns = [ path('', views.index), #修改端口默认主页 path('admin/', admin.site.urls), #管理员端 #登录注册 path('index/', views.index), path('check/', views.check), path('login/', views.login), path('register/', views.register), path('logout/', views.logout), #使用验证码 path('captcha', include('captcha.urls')), #报名app路由 path('homepage/',include('home.urls')), ]
2.4.2 试图设计
#login/views.py from django.shortcuts import render, redirect from . import models from .forms import userform,registerform from home.models import user_data # create your views here. def check(request): pass return render(request, 'login/hello.html') def index(request): pass return render(request, 'login/index.html') #加入sesson def login(request): #不允许重复登录 if request.session.get('is_login', none): return redirect('/index') if request.method == "post": login_form = userform(request.post) message = "请检查填写的内容!" if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] try: user = models.user.objects.get(name=username) if user.password == password: #往session字典内写入用户状态和数据 request.session['is_login'] = true request.session['user_id'] = user.id request.session['user_name'] = user.name return redirect('/index/') else: message = "密码不正确!" except: message = "用户不存在!" return render(request, 'login/login.html', locals()) login_form = userform() return render(request, 'login/login.html', locals()) def logout(request): if not request.session.get('is_login', none): # 如果本来就未登录,也就没有登出一说 return redirect("/index/") request.session.flush() # 或者使用下面的方法 # del request.session['is_login'] # del request.session['user_id'] # del request.session['user_name'] return redirect("/index/") def register(request): if request.session.get('is_login', none): # 登录状态不允许注册。 return redirect("/index/") if request.method == "post": register_form = registerform(request.post) message = "请检查填写的内容!" if register_form.is_valid(): # 获取数据 username = register_form.cleaned_data['username'] password1 = register_form.cleaned_data['password1'] password2 = register_form.cleaned_data['password2'] email = register_form.cleaned_data['email'] sex = register_form.cleaned_data['sex'] if password1 != password2: # 判断两次密码是否相同 message = "两次输入的密码不同!" return render(request, 'login/register.html', locals()) else: same_name_user = models.user.objects.filter(name=username) if same_name_user: # 用户名唯一 message = '用户已经存在,请重新选择用户名!' return render(request, 'login/register.html', locals()) same_email_user = models.user.objects.filter(email=email) if same_email_user: # 邮箱地址唯一 message = '该邮箱地址已被注册,请使用别的邮箱!' return render(request, 'login/register.html', locals()) message = "注册成功!" # 当一切都ok的情况下,创建新用户 # 创建用户信息//有问题:放在创建用户表后面会出现django pymysql.err.integrityerror: # (1062, "duplicate entry '' for key 'user_name'") new_user_data = user_data.objects.create() new_user_data.user_name = username new_user_data.user_true_name = '无' new_user_data.user_id = '无' new_user_data.user_school = '无' # new_user_data.user_f_score = 425 # new_user_data.user_s_score = 0 new_user_data.save() #创建用户表 new_user = models.user.objects.create() new_user.name = username new_user.password = password1 new_user.email = email new_user.sex = sex new_user.save() return redirect('/login/') # 自动跳转到登录页面 register_form = registerform() return render(request, 'login/register.html', locals())
3 个人信息及报名管理
(我将其放在了app名为home里面)
3.1 数据库模型设计
#home/models.py from django.db import models # create your models here. class exam_entry_table(models.model): #考点信息表 exam_id = models.charfield(verbose_name="考试编号",max_length=10) exam_type = models.charfield(verbose_name="考试类别",max_length=128) exam_point = models.charfield(verbose_name="考点",max_length=128) exam_time = models.charfield(verbose_name="考试时间",max_length=128) number = models.integerfield(verbose_name="容量") entry_number = models.integerfield(verbose_name="已报名数量",default=0) def __str__(self): return self.exam_point class meta: # ordering = ['c_time'] verbose_name = '考点' verbose_name_plural = '考点信息表' class user_entry_table(models.model): #用户考试信息表 email = models.emailfield(verbose_name="邮箱") exam_id = models.charfield(verbose_name="考试编号",max_length=10) exam_point = models.charfield(verbose_name="考点",max_length=128) def __str__(self): return self.email class meta: # ordering = ['c_time'] verbose_name = '用户考试信息' verbose_name_plural = '用户考试信息表' class user_data(models.model): #用户信息表 user_name = models.charfield(verbose_name="用户名",max_length=128, unique=true) user_true_name = models.charfield(verbose_name="真实姓名",max_length=128, null=true) user_id = models.charfield(verbose_name="身份证号",max_length=18, null=true) # user_img = user_school = models.charfield(verbose_name="在读学校",max_length=128) user_f_score = models.integerfield(verbose_name="四级成绩", default=0) user_s_score = models.integerfield(verbose_name="六级成绩", default=0) def __str__(self): return self.user_name class meta: # ordering = ['c_time'] verbose_name = '用户名' verbose_name_plural = '用户信息表'
3.2 注册模型
#home/admin.py from django.contrib import admin # register your models here. from . import models admin.site.register(models.exam_entry_table) #考点信息表 admin.site.register(models.user_entry_table) #用户考试信息表 admin.site.register(models.user_data) #用户信息表
3.3 创建表单
#home/forms.py from django import forms from captcha.fields import captchafield class userform(forms.form): username = forms.charfield(label="用户名", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) password = forms.charfield(label="密码", max_length=256, widget=forms.passwordinput(attrs={'class': 'form-control'})) captcha = captchafield(label='验证码') class registerform(forms.form): gender = ( ('male', "男"), ('female', "女"), ) username = forms.charfield(label="用户名", max_length=128, widget=forms.textinput(attrs={'class': 'form-control'})) password1 = forms.charfield(label="密码", max_length=256, widget=forms.passwordinput(attrs={'class': 'form-control'})) password2 = forms.charfield(label="确认密码", max_length=256, widget=forms.passwordinput(attrs={'class': 'form-control'})) email = forms.emailfield(label="邮箱地址", widget=forms.emailinput(attrs={'class': 'form-control'})) sex = forms.choicefield(label='性别', choices=gender) captcha = captchafield(label='验证码')
3.4 路由及视图设计
3.4.1 路由设计
这里用到的路劲较为复杂,采用项目urls和app的urls结合起来避免项目的urls过于拥挤。
先修改项目的urls.py文件:
#项目名/urls.py path('homepage/',include('home.urls')), #前面已经添加了,这里只做说明
再修改app中的urls.py文件:
#home/urls.py from django.contrib import admin from django.urls import path,include from home import views urlpatterns = [ path('test/', views.test), path('my_data/',views.mydate), #我的信息 path('query_results/',views.query), #查询报告信息 path('cet_4/',views.cet_4), #四级报名 path('cet_6/',views.cet_6), #六级报名 path('change_my_data/',views.updata), ]
3.4.2 试图设计
#home/views.py from django.shortcuts import render, redirect from . import models from .forms import user_entry, user_datas from django.http import httpresponse,httpresponseredirect # from django.contrib.auth.models import user from login.models import user # create your views here. def test(request): pass return render(request, 'home/test.html') #我的信息 def mydate(request): username = request.session.get('user_name', none) account = user.objects.get(name=username)#用户登录注册表 user = models.user_data.objects.get(user_name= username) return render(request,"home/myself.html",{"user":user, "account":account}) #修改我的信息 def updata(request): username = request.session.get('user_name', none) # print("-----------------------") # print(username) # print("-----------------------") user_da = models.user_data.objects.get(user_name=username) user = user.objects.get(name=username)#login_user表 if request.method == "post": userdata = user_datas(request.post) # print(userdata) if userdata.is_valid(): user_das = userdata.cleaned_data # user.user_name = user_da['user_name'] #用户无法修改用户名 user.email = user_das['email'] user_da.user_true_name = user_das['user_true_name'] user_da.user_id = user_das['user_id'] user_da.user_school = user_das['user_school'] user_da.save() user.save() # 四六级成绩无法修改 # user_datas.user_f_score = user_da['user_f_score'] # user_datas.user_s_score = user_da['user_s_score'] return redirect('/homepage/my_data/') else: userdata = user_datas(initial={"email":user.email,"user_name":user_da.user_name,"user_true_name":user_da.user_true_name,"user_id":user_da.user_id,"user_school":user_da.user_school}) return render(request, 'home/change_mydata.html', {"userdata":userdata}) #查询考试信息 还没完成,优化,条件判断 def query(request): username = request.session.get('user_name',none) user = user.objects.get(name=username) #用于判断用户是否报名 user_en = models.user_entry_table.objects.filter(email=user.email) # print("********************") # print(user_en) # print(user_en[0]) if user_en: # print(len(user_en)) if len(user_en)==1: user_entry = models.user_entry_table.objects.get(email=user.email) user_point = user_entry.exam_point user_eid = user_entry.exam_id exam_entry = models.exam_entry_table.objects.get(exam_point=user_point, exam_id=user_eid) return render(request, 'home/query1.html', {"exam_entry": exam_entry}) else: user_entry4 = models.user_entry_table.objects.get(email=user.email, exam_id=0) user_entry6 = models.user_entry_table.objects.get(email=user.email, exam_id=1) user_point4 = user_entry4.exam_point user_point6 = user_entry6.exam_point exam_entry4 = models.exam_entry_table.objects.get(exam_point=user_point4, exam_id=0) exam_entry6 = models.exam_entry_table.objects.get(exam_point=user_point6, exam_id=1) return render(request, 'home/query2.html', {"exam_entry4": exam_entry4, "exam_entry6":exam_entry6}) else: # message = "你还未报名!请先报名之后再来查看!" # return render(request, 'login/index.html', locals()) user_da = models.user_data.objects.get(user_name=user.name) school = user_da.user_school if school=='无': message = "请先更新你的学校信息!" return render(request, 'login/index.html', locals()) else: point = models.exam_entry_table.objects.filter(exam_point=school) if point: if len(point)==1: exam = models.exam_entry_table.objects.get(exam_point=school) return render(request, 'home/exam1.html', {"exam": exam}) else: exam4 = models.exam_entry_table.objects.get(exam_point=school, exam_id=0) exam6 = models.exam_entry_table.objects.get(exam_point=school, exam_id=1) return render(request, 'home/exam2.html', {"exam4": exam4, "exam6": exam6}) else: message="你的学校还未开放报名!详情请咨询学校相关部门!" return render(request, 'login/index.html', locals()) #四级报名 def cet_4(request): username = request.session.get('user_name', none) # 用户信息表,用户表,获取信息判断资格 user_da = models.user_data.objects.get(user_name=username) user = user.objects.get(name=username) if request.method == "post": cet4_form = user_entry(request.post) if cet4_form.is_valid(): # 四级考试对分数无要求 # 只需要获取报考考点信息即可 # email = cet4_form.cleaned_data['email'] exam_id = '0' exam_point = cet4_form.cleaned_data['exam_point'] #用与下面的考点判断 point = models.exam_entry_table.objects.filter(exam_point=exam_point, exam_id='0') # 用与下面的是否重复报名判断 entryer = models.user_entry_table.objects.filter(email=user.email, exam_id=exam_id) #判断个人信息是否完善 if user_da.user_true_name=='无'or user_da.user_id=='无': message="请先完善个人真实信息之后再来报名!" return render(request, 'home/cet_4.html', locals()) # 判断是否重复报名 # print("判断是否重复报名") elif entryer: message = "请勿重复报名!" return render(request, 'home/cet_4.html', locals()) elif point: # 考点存在 # print("没有重复报名") message = "报名成功!请按时参加考试!" # 创建一条数据 new_user = models.user_entry_table.objects.create() new_user.email = user.email new_user.exam_id = exam_id new_user.exam_point = exam_point new_user.save() # 考点容量减1,报考人数加1 exam_entry = models.exam_entry_table.objects.get(exam_point=exam_point, exam_id=exam_id) exam_entry.number -= 1 exam_entry.entry_number += 1 exam_entry.save() return render(request, 'home/cet_4.html', locals()) else: message = "该学校还未开放报名!详情请咨询学校相关部门!" return render(request, 'home/cet_4.html', locals()) cet4_form = user_entry(initial={"email": user.email, "user_name": user_da.user_name, "user_true_name": user_da.user_true_name, "user_id": user_da.user_id}) return render(request, 'home/cet_4.html', locals()) #六级报名: def cet_6(request): username = request.session.get('user_name', none) # 用户信息表,用户表,获取信息判断资格 user_da = models.user_data.objects.get(user_name=username) user = user.objects.get(name=username) if request.method == "post": cet6_form = user_entry(request.post) if cet6_form.is_valid(): # 只需要获取报考考点信息即可 # email = cet4_form.cleaned_data['email'] exam_id = '1' exam_point = cet6_form.cleaned_data['exam_point'] f_score = user_da.user_f_score # 用与下面的考点判断 point = models.exam_entry_table.objects.filter(exam_point=exam_point, exam_id=exam_id) # 用与下面的是否重复报名判断 entryer = models.user_entry_table.objects.filter(email=user.email, exam_id=exam_id) # 判断个人信息是否完善 if user_da.user_true_name=='无'or user_da.user_id=='无': message="请先完善个人真实信息之后再来报名!" return render(request, 'home/cet_6.html', locals()) # 判断是否重复报名 elif entryer: # print("判断是否重复报名") message = "请勿重复报名!" return render(request, 'home/cet_6.html', locals()) # 判断考点是否存在 elif point: # 考点存在 #判断四级成绩是否合格 if f_score >= 425: # print("报名成功!请按时参加考试!") # 创建一条数据 message = "报名成功!请按时参加考试!" new_user = models.user_entry_table.objects.create() new_user.email = user.email new_user.exam_id = exam_id new_user.exam_point = exam_point new_user.save() # 考点容量减1,报考人数加1 exam_entry = models.exam_entry_table.objects.get(exam_point=exam_point, exam_id=exam_id) exam_entry.number -= 1 exam_entry.entry_number += 1 exam_entry.save() return render(request, 'home/cet_6.html', locals()) else: message = "四级成绩大于425才能报名六级考试!" return render(request, 'home/cet_6.html', locals()) else: message = "该学校还未开放报名!详情请咨询学校相关部门!" return render(request, 'home/cet_6.html', locals()) cet6_form = user_entry( initial={"email": user.email, "user_name": user_da.user_name, "user_true_name": user_da.user_true_name, "user_id": user_da.user_id}) return render(request, 'home/cet_6.html', locals())
到这里基本的后端都实现了。
4 项目最终框架展示
5 总结
成果展示:
注册:
登录:
主页:
管理端:
数据库设计问题:
由于此次设计在开始之时没有设计完善,数据库各表之间的关联存在问题,例如本次报名系统而言,用户信息最好添加手机号码,我本打算使用邮箱即可,但是推荐使用联系更快的电话号码等等问题。
附录
整体项目包地址链接: https://pan.baidu.com/s/130ap7comp_u2q_dzaazuwa 提取码: nywa
总结
以上所述是小编给大家介绍的pycharm+django2.2+python3.6+mysql实现简单的考试报名系统,希望对大家有所帮助
上一篇: 从0到1构建vueSSR项目之路由的构建
下一篇: 低糖蔬菜有哪些