python3.6+django2.0开发一套学员管理系统


Posted in Python onMarch 03, 2018

1.在pycharm中新建project demo1 添加app01 点击create按钮完成新建

2.在demo项目目录下新建目录static,并在settings.py中追加代码:

STATICFILES_DIRS=(os.path.join(BASE_DIR, 'static'),)

3.在setting.py中添加模板路径:

TEMPLATES = [
 {
  'BACKEND': '...',
  'DIRS': [os.path.join(BASE_DIR, 'templates'),],
  'APP_DIRS': ...,
  'OPTIONS': {
   'context_processors': [
    ...
   ],
  },
 },
]

4.学员管理系统数据库设计:

在app01/model.py目录下建立 班级、老师、学生 、老师与班级关联表 四张表:

from django.db import models

# Create your models here.


class Classes(models.Model):
 '''
 班级表
 '''
 title=models.CharField(max_length=32)
 a=models.ManyToManyField('Teachers')


class Teachers(models.Model):
 '''
 老师表
 '''
 name=models.CharField(max_length=32)


class Students(models.Model):
 username=models.CharField(max_length=32)
 age=models.IntegerField()
 gender=models.BooleanField()
 cs=models.ForeignKey(Classes,on_delete=models.CASCADE)

在终端Terminal 项目目录下执行数据表更新命令:

python manage.py makemigrations
python manage.py migrate

至此生成了四张数据表,可以在pycharm中,点开右上角的Database面板,然后将项目中templates目录下边的db.sqlite3鼠标拖拽到Database面板下,对新创建的数据表进行查看。

5.学员管理系统之班级管理:

为了方便分别操作班级、老师、学生相关的业务,将app01目录下的views.py 删掉,在app01目录下新建目录views,并在views目录下 新建classes.py teachers.py students.py。

1.在classes.py 中写 get_classes add_classes del_classes edit_classes四个函数,完成对 班级数据 的增删改查:

from django.shortcuts import render,redirect
from app01 import models


def get_classes(request):
 cls_list = models.Classes.objects.all()
 return render(request,'get_classes.html',{'cls_list':cls_list})

def add_classes(request):
 if request.method=='GET':
  return render(request,'add_classes.html')
 elif request.method=='POST':
  title=request.POST.get('title','')
  models.Classes.objects.create(title=title)
  return redirect('/classes.html')

def del_classes(request):
 nid=request.GET.get('nid','')
 models.Classes.objects.filter(id=nid).delete()
 return redirect('/classes.html')

def edit_classes(request):
 if request.method=="GET":
  nid = request.GET.get('nid', '')
  obj=models.Classes.objects.get(id=nid)
  return render(request,'edit_classes.html',{'obj':obj})
 elif request.method=="POST":
  nid=request.POST.get('nid','')
  title=request.POST.get('xxoo','')
  models.Classes.objects.filter(id=nid).update(title=title)
  return redirect('/classes.html')

2.在urls.py 中配置url路由:

from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers

urlpatterns = [
 path('admin/', admin.site.urls),
 path('classes.html', classes.get_classes),
 path('add_classes.html', classes.add_classes),
 path('del_classes.html', classes.del_classes),
 path('edit_classes.html', classes.edit_classes),
 # path('teachers.html', teachers.get_teachers),
 # path('students.html', students.get_studernts),

]

3.在template目录下建立所需的html页面文件:

get_classes.html

DOCTYPE html>
<html lang="en">
<head>
 <style>
  tr td{ border:1px solid #000;text-align:center;}
 </style>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>

<div>
 <table>
  <thead>
   <tr>
    <th>ID</th> <th>名称</th> <th>操作</th>
   </tr>
  </thead>
  <tbody>
   {% for row in cls_list %}
   <tr>
    <td>{{ row.id }}</td>
    <td>{{ row.title }}</td>
    <td><a href="/del_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >删除</a>
     |<a href="/edit_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >编辑</a>
    </td>
   </tr>
   {% endfor %}
  </tbody>
 </table>
</div>

<div><a href="/add_classes.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>

add_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<form action="/add_classes.html" method="post">
 {% csrf_token %}
 <input type="text" name="title">
 <input type="submit" value="提交">
</form>
</body>
</html>

edit_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<form method="post" action="/edit_classes.html">
 {% csrf_token %}
 <input type="hidden" name="nid" value="{{ obj.id }}">
 <input type="text" name="xxoo" value="{{ obj.title }}">
 <input type="submit" value="提交">
</form>

</body>
</html>

6.学员管理系统之学员管理: 1.在students.py 中写 get_students add_students del_students edit_students 四个函数,完成对 学生数据 的增删改查:

from django.shortcuts import render,redirect
from app01 import models


def get_students(request):
 stu_list=models.Students.objects.all()
 return render(request,'get_students.html',{'stu_list':stu_list})


def add_students(request):
 if request.method=='GET':
  cs_list=models.Classes.objects.all()
  return render(request,'add_students.html',{'cs_list':cs_list})
 elif request.method=='POST':
  u=request.POST.get('username','')
  a=request.POST.get('age','')
  g=request.POST.get('gender','')
  c=request.POST.get('cs','')
  models.Students.objects.create(
   username=u,
   age=a,
   gender=g,
   cs_id=c
  )
  return redirect('/students.html')


def del_students(request):
 nid = request.GET.get('nid', '')
 models.Students.objects.filter(id=nid).delete()
 return redirect('/students.html')


def edit_students(request):
 if request.method=="GET":
  nid = request.GET.get('nid', '')
  obj=models.Students.objects.get(id=nid)
  cs_list = models.Classes.objects.all()
  return render(request,'edit_students.html',{'obj':obj,'cs_list':cs_list})
 elif request.method=="POST":
  nid=request.POST.get('nid','')
  u = request.POST.get('username', '')
  a = request.POST.get('age', '')
  g = request.POST.get('gender', '')
  c = request.POST.get('cs', '')
  models.Students.objects.filter(id=nid).update(
   username=u,
   age=a,
   gender=g,
   cs_id=c)
  return redirect('/students.html')

2.在urls.py 中配置url路由: 

from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers

urlpatterns = [
 path('admin/', admin.site.urls),

 path('classes.html', classes.get_classes),
 path('add_classes.html', classes.add_classes),
 path('del_classes.html', classes.del_classes),
 path('edit_classes.html', classes.edit_classes),

 path('students.html', students.get_students),
 path('add_students.html', students.add_students),
 path('del_students.html', students.del_students),
 path('edit_students.html', students.edit_students),
 # path('teachers.html', teachers.get_teachers),
]

3.在template目录下建立所需的html页面文件:

get_students.html

<!DOCTYPE html>
<html lang="en">
<head>
 <style>
  tr td{ border:1px solid #000;text-align:center;}
 </style>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>

<div>
 <table>
  <thead>
   <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>性别</th>
    <th>班级</th>
    <th>操作</th>
   </tr>
  </thead>
  <tbody>
   {% for row in stu_list %}
   <tr>
    <td>{{ row.id }}</td>
    <td>{{ row.username }}</td>
    <td>{{ row.age }}</td>
    <td>{{ row.gender }}</td>
    <td>{{ row.cs.title }}</td>
    <td><a href="/del_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >删除</a>
     |<a href="/edit_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >编辑</a>
    </td>
   </tr>
   {% endfor %}
  </tbody>
 </table>
</div>

<div><a href="/add_students.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>

add_students

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<h1>添加用户</h1>
<form method="post" action="/add_students.html">
 {% csrf_token %}
 <p><input type="text" name="username" placeholder="用户名"></p>
 <p><input type="text" name="age" placeholder="年龄"></p>
 <p>
  男<input type="radio" name="gender" value="1">
  女<input type="radio" name="gender" value="0">
 </p>
 <p>
  <select name="cs">
   {% for row in cs_list %}
   <option value="{{ row.id }}">{{ row.title }}</option>
   {% endfor %}
  </select>
 </p>
 <p><input type="submit" value="提交"></p>
</form>
</body>
</html>

edit_students.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<h1>编辑用户</h1>
<form method="post" action="/edit_students.html">
 {% csrf_token %}
 <input type="hidden" name="nid" value="{{ obj.id }}">
 <p><input type="text" name="username" placeholder="用户名"></p>
 <p><input type="text" name="age" placeholder="年龄"></p>
 <p>
  男<input type="radio" name="gender" value="1">
  女<input type="radio" name="gender" value="0">
 </p>
 <p>
  <select name="cs">
   {% for row in cs_list %}
   <option value="{{ row.id }}">{{ row.title }}</option>
   {% endfor %}
  </select>
 </p>
 <p><input type="submit" value="提交"></p>
</form>
</body>
</html>

7.学员管理系统之给班级分配老师:

在teachers数据表中增加一些老师信息:

在pycharm右上角的Database打开面板,然后将template目录下边的db.splte3鼠标拖入到Database面板中,打开db==》app01_teachers表

点击“+”,然后填入老师信息,然后点击有“DB”标志的向上箭头,进行数据保存。

1.在classes.py中增加set_teachers函数

def set_teachers(request):
 if request.method=='GET':
  nid=request.GET.get('nid','')
  cls_obj=models.Classes.objects.get(id=nid)
  cls_teacher_list=cls_obj.a.all()
  all_teacher_list=models.Teachers.objects.all()
  return render(request,'set_teachers.html',{
   'cls_teacher_list':cls_teacher_list,
   'all_teacher_list':all_teacher_list,
   'nid':nid,
  })
 elif request.method=='POST':
  nid = request.POST.get('nid', '')
  ids_str=request.POST.getlist('teacher_id','')
  ids_int=[]
  for i in ids_str:
   i=int(i)
   ids_int.append(i)
  obj=models.Classes.objects.get(id=nid)
  obj.a.set(ids_int)
  return redirect('/classes.html')

2.在urls.py 中配置url路由: 

from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers

urlpatterns = [
 path('admin/', admin.site.urls),

 path('classes.html', classes.get_classes),
 path('add_classes.html', classes.add_classes),
 path('del_classes.html', classes.del_classes),
 path('edit_classes.html', classes.edit_classes),

 path('students.html', students.get_students),
 path('add_students.html', students.add_students),
 path('del_students.html', students.del_students),
 path('edit_students.html', students.edit_students),

 path('set_teachers.html', classes.set_teachers),
]
 

3.在template目录下建立所需的html页面文件:

set_teachers.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<form action="/set_teachers.html" method="post">
 <input type="hidden" name="nid" value="{{ nid }}">
 {% csrf_token %}
 <select multiple size="10" name="teacher_id">

  {% for item in all_teacher_list %}
   {% if item in cls_teacher_list %}
   <option value="{{ item.id }}" selected="selected">{{ item.name }}</option>
   {% else %}
   <option value="{{ item.id }}">{{ item.name }}</option>
   {% endif %}
  {% endfor %}
 </select>
 <input type="submit" value="提交">
</form>
</body>
</html>

对get_classes.html进行增添修改为:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>

<div>
 <table>
  <thead>
   <tr>
    <th>ID</th> <th>名称</th> <th>任课老师</th> <th>操作</th>
   </tr>
  </thead>
  <tbody>
   {% for row in cls_list %}
   <tr>
    <td>{{ row.id }}</td>
    <td>{{ row.title }}</td>
    <td>
     {% for item in row.a.all %}
      <span>{{ item.name }}</span>
      {% endfor %}
    </td>
    <td><a href="/del_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >删除</a>
     |<a href="/edit_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >编辑</a>
     |<a href="/set_teachers.html?nid={{ row.id }}" rel="external nofollow" >分配老师</a>
    </td>
   </tr>
   {% endfor %}
  </tbody>
 </table>
</div>

<div><a href="/add_classes.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>

8.初识Ajax

Ajax是异步传输方式,偷偷的向后台发请求,不引起页面刷新,下面通过一个小例子来认识Ajax这种数据传输方式。

首先下载jQuery导入项目下的static目录下

1.在app01/Views目录下新建ajax.py

from django.shortcuts import render,redirect,HttpResponse


def ajax1(request):
 return render(request,'ajax1.html')


def ajax2(request):
 u=request.GET.get('username')
 p=request.GET.get('password')
 return HttpResponse('我愿意')

2.在urls.py中配置相关路由

from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers,ajax

urlpatterns = [
 path('admin/', admin.site.urls),

 path('classes.html', classes.get_classes),
 path('add_classes.html', classes.add_classes),
 path('del_classes.html', classes.del_classes),
 path('edit_classes.html', classes.edit_classes),

 path('students.html', students.get_students),
 path('add_students.html', students.add_students),
 path('del_students.html', students.del_students),
 path('edit_students.html', students.edit_students),

 path('set_teachers.html', classes.set_teachers),

 path('ajax1.html', ajax.ajax1),
 path('ajax2.html', ajax.ajax2),
]

3.在template目录下新建ajax1.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
  .btn{
   display: inline-block;
   padding: 5px 15px;
   background-color: coral;
   color: white;
   cursor: pointer;
  }
 </style>
</head>
<body>
<div>
 <input placeholder="用户名" type="text">
 <input placeholder="密码" type="password">
 <div class="btn">提交</div>
</div>
<script src="/static/jquery-3.3.1.js"></script>
<script>
 function submitForm() {
  var u=$('#username').val();
  var p=$('#password').val();
  $.ajax({
   url:'ajax2.html',
   type:'GET',
   data:{username:u,password:p},
   success:function (arg) {
    //回调函数 arg是服务器返回的字符串
    console.log(arg)
   }

  })

 }
</script>
</body>
</html>

9.学员管理系统之Ajax删除学员: 1.在ajax.py中增加ajax4函数

from app01 import models
def ajax4(request):
 nid=request.GET.get('nid')
 msg='成功'
 try:
  models.Students.objects.get(id=nid).delete()
 except Exception as e:
  msg=str(e)
 return HttpResponse(msg)

2.在urls.py中配置相关路由

from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers,ajax

urlpatterns = [
 path('admin/', admin.site.urls),

 path('classes.html', classes.get_classes),
 path('add_classes.html', classes.add_classes),
 path('del_classes.html', classes.del_classes),
 path('edit_classes.html', classes.edit_classes),

 path('students.html', students.get_students),
 path('add_students.html', students.add_students),
 path('del_students.html', students.del_students),
 path('edit_students.html', students.edit_students),

 path('set_teachers.html', classes.set_teachers),

 path('ajax1.html', ajax.ajax1),
 path('ajax2.html', ajax.ajax2),
 path('ajax4.html', ajax.ajax4),
]

3.对get_students.html进行添加修改:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>

<div>
 <table>
  <thead>
   <tr>
    <th>ID</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>性别</th>
    <th>班级</th>
    <th>操作</th>
   </tr>
  </thead>
  <tbody>
   {% for row in stu_list %}
   <tr nid="{{ row.id }}">
    <td>{{ row.id }}</td>
    <td>{{ row.username }}</td>
    <td>{{ row.age }}</td>
    <td>{{ row.gender }}</td>
    <td>{{ row.cs.title }}</td>
    <td><a href="/del_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >删除</a>
     |<a href="#" rel="external nofollow" >Ajax删除</a>
     |<a href="/edit_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >编辑</a>
    </td>
   </tr>
   {% endfor %}
  </tbody>
 </table>
</div>

<div><a href="/add_students.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
<script src="/static/jquery-3.3.1.js"></script>
<script>
 function removeStudent(ths) {
  var nid=$(ths).parent().parent().attr('nid');
  $.ajax({
   url:'/ajax4.html',
   type:'GET',
   data:{nid:nid},
   success:function (arg) {
    if (arg=='成功'){
     window.location.reload();
    }else{
     alert(arg);
    }
   }
  })
 }
</script>
</html>
Python 相关文章推荐
Python 备份程序代码实现
Mar 06 Python
Python 将pdf转成图片的方法
Apr 23 Python
Flask框架通过Flask_login实现用户登录功能示例
Jul 17 Python
在python中将字符串转为json对象并取值的方法
Dec 31 Python
python opencv 读取本地视频文件 修改ffmpeg的方法
Jan 26 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
Feb 21 Python
pytorch 加载(.pth)格式的模型实例
Aug 20 Python
使用Python进行中文繁简转换的实现代码
Oct 18 Python
python argparser的具体使用
Nov 10 Python
解决python replace函数替换无效问题
Jan 18 Python
anaconda3安装及jupyter环境配置全教程
Aug 24 Python
Python 下载Bing壁纸的示例
Sep 29 Python
python爬虫面试宝典(常见问题)
Mar 02 #Python
Python基于Flask框架配置依赖包信息的项目迁移部署
Mar 02 #Python
谈谈python中GUI的选择
Mar 01 #Python
纯python实现机器学习之kNN算法示例
Mar 01 #Python
用python与文件进行交互的方法
Mar 01 #Python
python爬虫爬取快手视频多线程下载功能
Feb 28 #Python
python爬取m3u8连接的视频
Feb 28 #Python
You might like
五款常用mysql slow log分析工具的比较分析
2011/05/22 PHP
批量获取memcache值并按key的顺序返回的实现代码
2011/06/14 PHP
php中将图片gif,jpg或mysql longblob或blob字段值转换成16进制字符串
2011/08/23 PHP
PHP实现简单的新闻发布系统实例
2015/07/28 PHP
LNMP部署laravel以及xhprof安装使用教程
2017/09/14 PHP
PHP 应用容器化以及部署方法
2018/02/12 PHP
laravel5.1框架model类查询的实现方法
2019/10/08 PHP
laravel实现一个上传图片的接口,并建立软链接,访问图片的方法
2019/10/12 PHP
jQuery学习基础知识小结
2010/11/25 Javascript
Iframe自适应高度绝对好使的代码 兼容IE,遨游,火狐
2011/01/27 Javascript
js函数的引用, 关于内存的开销
2012/09/17 Javascript
使用insertAfter()方法在现有元素后添加一个新元素
2014/05/28 Javascript
javascript实现鼠标拖动改变层大小的方法
2015/04/30 Javascript
轻松学习jQuery插件EasyUI EasyUI创建CRUD应用
2015/11/30 Javascript
javascript时间排序算法实现活动秒杀倒计时效果
2021/01/28 Javascript
轻量级富文本编辑器wangEditor结合vue使用方法示例
2018/10/10 Javascript
vue实现按钮切换图片
2021/01/20 Vue.js
[05:29]2014DOTA2国际邀请赛 赛后专访:LGDNewbee顺利过关
2014/07/13 DOTA
python 从远程服务器下载东西的代码
2013/02/10 Python
python实现的一个p2p文件传输实例
2014/06/04 Python
删除pycharm鼠标右键快捷键打开项目的操作
2021/01/16 Python
css3动画效果小结(推荐)
2016/07/25 HTML / CSS
canvas实现图片镜像翻转的2种方式
2020/07/22 HTML / CSS
斯福泰克软件测试面试题
2015/02/16 面试题
后勤人员岗位职责
2013/12/17 职场文书
运动会致辞稿50字
2014/02/04 职场文书
委托协议书范本
2014/04/22 职场文书
运动会演讲稿100字
2014/08/25 职场文书
公安机关党的群众路线教育实践活动剖析材料
2014/10/10 职场文书
社区学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
部门经理迟到检讨书
2015/02/16 职场文书
师德师风主题教育活动总结
2015/05/07 职场文书
2016寒假假期总结
2015/10/10 职场文书
创业计划书之网吧
2019/10/10 职场文书
导游词之张家口
2019/12/13 职场文书
利用正则表达式匹配浮点型数据
2022/05/30 Java/Android