博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python/Django(增、删、改、查)操作
阅读量:5066 次
发布时间:2019-06-12

本文共 5146 字,大约阅读时间需要 17 分钟。

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     
5 Title 6 7 8

班级列表

9
10
添加11
12
13
14
15
16
17
18
19
20 21 {% for row in class_list%} for 循环22
23
24
25
30
31 {% endfor %} 结束for循环 (开始结束一定要共存)32
ID 班级名称 操作
{
{ row.cid }}
{
{ row.cname }}
26 编辑27 |28 删除29
33 34

运行效果:

添加:

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     
5 Title 6 7 8

添加班级

9
10

班级名称:

11
12
13 14

运行结果:

 

删除:

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代码:

    
Title

班级修改

班级名称:

{
#
#} {
# {% for row in ste %}#} {
# {
{ row }}
#} {
# {% endfor %}#}

运行效果:

 

转载于:https://www.cnblogs.com/guobaoyuan/p/7018970.html

你可能感兴趣的文章
一道算法面试题(Lowerest Common Ancestor) 的几种解法
查看>>
数组的逆序出输出、查找一个元素
查看>>
Construct Binary Tree from Preorder and Inorder Traversal -- LeetCode
查看>>
【慢慢学Android】:4.Service的开机启动
查看>>
笔记--Day2--python基础2
查看>>
Vue,品牌列表案例(仅添加)
查看>>
数学(概率)CodeForces 626D:Jerry's Protest
查看>>
并发编程 19—— 显式的Conditon 对象
查看>>
软件测试必看的5本
查看>>
程序员必备的600单词
查看>>
hipster
查看>>
java:POI导出excel
查看>>
Web开发感悟:数据绑定是一种技术,更是一门艺术
查看>>
删除标题和边框
查看>>
JAVA第九次作业
查看>>
字符串反转,如将 '12345678' 变成 '87654321'
查看>>
Docker 安装 PHP+Nginx
查看>>
(转)MySQL排序原理与案例分析
查看>>
Miller-Rabin素数测试算法(POJ1811Prime Test)
查看>>
子路由配置
查看>>