python/Django(增、删、改、查)操作
我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作。
一、创建Django项目(每创建一个项目都要进行以下设置)
1、如图所示,创建一个Project
2、红色区域是创建的项目名称,蓝色区域是创建目录
3、如上图,把设置中这个注释掉
4、在设置文件中设置HTML文件夹路径
5、设置完文件夹路径,创建一个文件夹
二、连接数据库进行操作
建议:创建一个文件夹存放python代码,最后导入到urls.py文件中,如图:
在urls.py文件中设置url和相应的函数:
1 urlpatterns = [2 # url(r'^admin/', admin.site.urls),3 url(r'^classes/', stter.classes),4 url(r'^add_class/', stter.add_class),5 url(r'^del_class/', stter.del_class),6 url(r'^update_class/', stter.update_class),7 ]
查看数据库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 8001Performing 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:15Django 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代码:
1 2 3 4 5Title 6 7 8班级列表
910 添加1112
ID | 16班级名称 | 17操作 | 18
---|---|---|
{ { row.cid }} | 24{ { row.cname }} | 2526 编辑27 |28 删除29 | 30
运行效果:
添加:
1 def add_class(request): ##定义添加数据库函数 2 if request.method == 'GET': 3 return render(request,'add_class.html') 4 else: 5 print(request.POST) 6 v=request.POST.get('cname') 7 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8') 8 cursor = conn.cursor() 9 cursor.execute("insert into class(cname) VALUES(%s)",[v,])10 conn.commit()11 cursor.close()12 conn.close()13 return redirect('/classes/')14 ---------------------------------------------------------------15 Performing system checks...16 17 System check identified no issues (0 silenced).18 19 You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.20 Run 'python manage.py migrate' to apply them.21 June 15, 2017 - 17:08:1522 Django version 1.11.2, using settings 's4day65.settings'23 Starting development server at http://127.0.0.1:8001/24 Quit the server with CTRL-BREAK.25 [15/Jun/2017 17:10:00] "GET /classes/ HTTP/1.1" 200 139626 ===============================
添加的HTML代码:
1 2 3 4 513 14Title 6 7 8添加班级
9
运行结果:
删除:
1 def del_class(request): ##定义删除数据库函数2 nid=request.GET.get('nid')3 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')4 cursor = conn.cursor()5 cursor.execute("delete from class WHERE cid=%s",[nid,])6 conn.commit()7 cursor.close()8 conn.close()9 return redirect('/classes/')
运行结果:
修改:
1 def update_class(request): 2 if request.method =='GET': 3 nid = request.GET.get('nid') 4 # name=request.POST.get('cname') 5 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8') 6 cursor = conn.cursor() 7 cursor.execute("select class.cid,class.cname from class where cid=%s", [nid,]) 8 ste = cursor.fetchone() 9 return render(request, 'update_class.html', { 'ste': ste})10 else:11 id=request.GET.get('nid')12 name=request.POST.get('cname')13 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', database='lian', charset='utf8')14 cursor = conn.cursor()15 cursor.execute("update class set cname=%s where cid = %s",[name,id,])16 conn.commit()17 cursor.close()18 conn.close()19 return redirect('/classes/')
修改HTML代码:
#} { # {% for row in ste %}#} { # { { row }}#} { # {% endfor %}#}Title 班级修改
运行效果: