python/Django(增、删、改、查)操作
我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作。
一、创建Django项目(每创建一个项目都要进行以下设置)
1、如图所示,创建一个Project
2、红色区域是创建的项目名称,蓝色区域是创建目录
3、如上图,把设置中这个注释掉
4、在设置文件中设置HTML文件夹路径
5、设置完文件夹路径,创建一个文件夹
二、连接数据库进行操作
建议:创建一个文件夹存放python代码,最后导入到urls.py文件中,如图:
在urls.py文件中设置url和相应的函数:
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^classes/', stter.classes),
url(r'^add_class/', stter.add_class),
url(r'^del_class/', stter.del_class),
url(r'^update_class/', stter.update_class),
]
查看数据库class表的内容:
from django.shortcuts import render,redirect
##导入django模块下的返回方式和跳转模块
import pymysql
##导入pymysql模块
def classes(request): ##定义查看数据库函数
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('select * from class')
class_list = cursor.fetchall()
cursor.close()
conn.close()
return render(request,'classes.html',{'class_list':class_list})
返回给用户(经过classes.html和用户浏览器渲染内容){class_list}是传值到html中使用
------------------------------------------------------------------------
"D:\pycharm\PyCharm 2017.1\bin\runnerw.exe" D:\python3.6\python.exe E:/s4day65/manage.py runserver 8001
Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 15, 2017 - 17:08:15
Django version 1.11.2, using settings 's4day65.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CTRL-BREAK.
===============运行正常================
查看HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>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> {% for row in class_list%} for 循环
<tr>
<td>{{ row.cid }}</td>
<td>{{ row.cname }}</td>
<td>
<a href="/update_class/?nid={{ row.cid }}">编辑</a>
|
<a href="/del_class/?nid={{ row.cid }}">删除</a>
</td>
</tr>
{% endfor %} 结束for循环 (开始结束一定要共存)
</table>
</body>
</html>
运行效果:
添加:
def add_class(request): ##定义添加数据库函数
if request.method == 'GET':
return render(request,'add_class.html')
else:
print(request.POST)
v=request.POST.get('cname')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')
cursor = conn.cursor()
cursor.execute("insert into class(cname) VALUES(%s)",[v,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
---------------------------------------------------------------
Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 15, 2017 - 17:08:15
Django version 1.11.2, using settings 's4day65.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CTRL-BREAK.
[15/Jun/2017 17:10:00] "GET /classes/ HTTP/1.1" 200 1396
===============================
添加的HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>添加班级</h1>
<form method="POST" action="/add_class/">
<p>班级名称:<input type="text" name="cname"></p>
<input type="submit" value="提交">
</form>
</body>
</html>
运行结果:
删除:
def del_class(request): ##定义删除数据库函数
nid=request.GET.get('nid')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')
cursor = conn.cursor()
cursor.execute("delete from class WHERE cid=%s",[nid,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
运行结果:
修改:
def update_class(request):
if request.method =='GET':
nid = request.GET.get('nid')
# name=request.POST.get('cname')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')
cursor = conn.cursor()
cursor.execute("select class.cid,class.cname from class where cid=%s", [nid,])
ste = cursor.fetchone()
return render(request, 'update_class.html', {'ste': ste})
else:
id=request.GET.get('nid')
name=request.POST.get('cname')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')
cursor = conn.cursor()
cursor.execute("update class set cname=%s where cid = %s",[name,id,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
修改HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>班级修改</h1>
<form method="POST" action="/update_class/?nid={{ ste.0 }}">
<p>班级名称:<input type="text" name="cname" value="{{ ste.1 }}"></p>
<input type="submit" value="提交">
{# </form>#}
{# {% for row in ste %}#}
{# <a>{{ row }}</a>#}
{# {% endfor %}#}
</form>
</body>
</html>