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),
]