Django之学员管理一
2019-05-22 06:33:43来源:博客园 阅读 ()
Django之学员管理一
建表结构:
#班级表class: id title 1 五年一班 2 五年二班 3 五年三班 4 五年四班 #学生表student: id name 班级ID(FK外键) 1 George 1 2 Jack 2 3 Tom 2 4 Alex 1 #老师表teacher: id name 1 Crystal 2 Lee 3 Iron Man #老师班级关系表teacher_and_class: id 老师ID 班级ID 1 1 1 2 1 2 3 2 3 4 2 4 5 3 1 6 3 2
操作单表:班级表和老师表
views.py
from django.shortcuts import render,redirect import pymysql def classes(request): conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select id,title from class") class_list = cursor.fetchall() cursor.close() conn.close() return render(request,'classes.html',{'class_list': class_list}) def add_class(request): if request.method == "GET": return render(request,'add_class.html') else: print(request.POST) v = request.POST.get('title') conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("insert into class(title) values(%s)",[v,]) conn.commit() cursor.close() conn.close() return redirect('/classes/') def del_class(request): nid = request.GET.get('nid') conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("delete from class where id=%s",[nid,]) conn.commit() cursor.close() conn.close() return redirect('/classes/') def edit_class(request): if request.method == "GET": nid = request.GET.get('nid') conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select id,title from class where id = %s",[nid,]) result = cursor.fetchone() cursor.close() conn.close() print(result) return render(request,'edit_class.html',{'result':result}) else: nid = request.GET.get('nid') title = request.POST.get('title') conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("update class set title=%s where id = %s",[title,nid,]) conn.commit() cursor.close() conn.close() return redirect('/classes/')
classes.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>班级列表</h1> <div> <a href="/add_class/">添加</a> </div> <table> <thead> <tr> <th>ID</th> <th>班级名称</th> <th>操作</th> </tr> </thead> <tbody> {% for row in class_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.title }}</td> <td> <a href="/edit_class/?nid={{ row.id }}">编辑</a> | <a href="/del_class/?nid={{ row.id }}">删除</a> </td> </tr> {% endfor %} </tbody> </table> </body> </html>
add_class.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>添加班级</h1> <form method="POST" action="/add_class/"> <p>班级名称:<input type="text" name="title" /></p> <input type="submit" value="提交" /> </form> </body> </html>
edit_class.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>编辑班级</h1> <form method="POST" action="/edit_class/?nid={{ result.id }}"> <p>班级名称:<input type="text" name="title" value="{{ result.title }}" /></p> <input type="submit" value="提交" /> </form> </body> </html>
url.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^classes/', views.classes), url(r'^add_class/', views.add_class), url(r'^del_class/', views.del_class), url(r'^edit_class/', views.edit_class), ]
原文链接:https://www.cnblogs.com/george92/p/10886681.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:day23 03 组合的例子
- Django项目中使用qq第三方登录。 2019-08-13
- Python之装饰器笔记 2019-08-13
- Django和前端用ajax传输json等数据 2019-08-13
- Django基本知识 2019-08-13
- JavaScript获取Django模板中指定键值的数据,使用过滤器 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash