模拟学生管理系统,创建mysql数据库。
由于我是在Ubuntu虚拟机打的代码,数据库也在虚拟机上,
mysql,默认只有本机可以访问,远程需要注释掉这一句
etc/mysql/mysql.con.d/mysql.cnf 当前版本是mysql 5.7
#不同版本路径可能不一样,网上有的是“/etc/my.cnf,
注释这一行:bind-address=127.0.0.1 ==> #bind-address=127.0.0.1
重启: service mysqld restart
service mysql restart (5.5.7版本命令)
service 启动:
[root@localhost /]# service mysqld start (5.0版本是mysqld)
[root@szxdb etc]# service mysql start (5.5.7版本是mysql)
/etc/inint.d/mysqld start
停止:service mysqld stop
**
查询
**
一直在urls.py上写函数是不行的,需要整理好对应的文件夹
创建文件夹及文件mysite – app01 --views.py
urls.py添加
from app01 import views #加载文件夹,下面调用函数才能找到
urlpatterns = [
#path('admin/', admin.site.urls),
path('login/',login),
path('index/',index),
#url(r'^class/',views.classes), 这个是以前版本,path是新版本的
path('classes/',views.classes),
path('add_class/',views.add_class),
]
views.py
from django.shortcuts import HttpResponse,render,redirect #导入模块
import pymysql
def classes(request):
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pydata',charset='utf8')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id,title from class")
class_list = cursor.fetchall()
# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
#print(class_list)
return render(request,'classes.html',{'class_list':class_list})
下面如没说明修改,则为添加的函数
创建html文件,mystie – templates – classes.html
<div>
<a href="/add_class/">添加</a>{{ add_se }}
</div>
<table border="1" >
<thead>
<tr>
<th>班级ID</th>
<th>班级名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for msg in class_list %}
<tr>
<td>{{ msg.id }}</td>
<td>{{ msg.title }}</td><td><a href="">编辑</a> | <a href="/del_class/?nid={{ msg.id }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
添加
views.py
def add_class(request):
if request.method == 'GET':
return render(request, 'add_class.html')
else:
v = request.POST.get('title') #add_class提交的name="title"的数据
if len(v)>0: #如果提交的数据不为空,才能就行操作,否则返回"班级名称不能为空"
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pydata', 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/')
else:
return render(request, 'add_class.html',{'msg': '班级名称不能为空'})
删除
def del_class(request):
nid = request.GET.get('nid')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pydata', 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/')
urls.py的 urlpatterns,增加:path(‘del_class/’,views.del_class),
编辑
新建mysite – templates --edit.html
<body>
<h1>编辑班级</h1>
<form method="post" action="/edit_class/">
<p>班级ID:{{ result.id }}</p>
<input style="display: none" name="id" value="{{ result.id }}" />
{# style="display: none" 不显示 显示但不可修改的ID值 #}
<p>班级名称:<input type="text" name="title" value="{{ result.title }}" /></p>
<input type="submit" value="提交">
</form>
</body>
urls.py的 urlpatterns,增加:path(‘edit_class/’,views.edit_class),
views.py增加函数
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='root', db='pydata', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id ,title from class where id = %s", [nid, ])
result = cursor.fetchone() #conn.commit()有传值替换这个
#print(result)
cursor.close()
conn.close()
return render(request, 'edit_class.html', {'result': result})
else:
nid = request.POST.get('id')
title = request.POST.get('title')
print(nid,title)
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='pydata', 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/')
来源:CSDN
作者:m0_37735015
链接:https://blog.csdn.net/m0_37735015/article/details/104530107