url.py文件(路由分配url)
from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
path('classes/',views.classes),
path('add_class/',views.add_class),
path('del_class/',views.del_class),
path('edit_class/',views.edit_class),
]
函数定义
from django.shortcuts import render,redirect
import pymysql
def classes(request):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('select * from class')
classes_list=cursor.fetchall()
cursor.close()
conn.close()
return render(request, 'classes.html', {'classes_list':classes_list})
def add_class(request):
if request.method=='GET':
return render(request, 'add_class.html')
else:
title=request.POST.get('title')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('insert into class(title) values(%s)',[title,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
def del_class(request):
id=request.GET.get('id')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('delete from class where id=%s', [id, ])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
def edit_class(request):
if request.method == 'GET':
id=request.GET.get('id')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('select id,title from class where id=%s', [id, ])
result=cursor.fetchone()
cursor.close()
conn.close()
return render(request,'edit_class.html',{'result':result})
else:
id=request.GET.get('id')
title=request.POST.get('title')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='collage', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('update class set title=%s where id=%s', [title,id, ])
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>
<td>ID</td>
<td>班级名称</td>
<td>操作</td>
</tr>
</thead>
<tbody>
{% for row in classes_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.title }}</td>
<td>
<a href="/edit_class/?id={{ row.id }}">编辑</a>
<a href="/del_class/?id={{ 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/?id={{ result.id }}">
<p>班级名称:<input type="text" name="title" value="{{ result.title }}"/></p>
<input type="submit" value="提交">
</form>
</body>
</html>