## 前言
本篇使用Python Web框架Django连接和操作MySQL数据库学生信息管理系统(SMS),主要包含对学生信息增删改查功能,旨在快速入门Python Web,少走弯路。效果演示在项目实战最后一节,文章结尾有整个项目的源码地址。
## 开发环境
- 开发工具:Pycharm 2020.1
- 开发语言:Python 3.8.0
- Web框架:Django 3.0.6
- 数据库:MySQL5.7
- 操作系统:Windows 10
## 项目实战
### 1. 创建项目
File->New Project->Django
![](https://i.loli.net/2020/05/09/vV3XJCQqTKeUZ5b.png)
稍等片刻,项目的目录结构如下图
![](https://i.loli.net/2020/05/09/4RAteIszSnZuqK9.png)
项目创建后确认是否已安装Django和mysqlclient解释器,如何确认?file->Settings
![](https://i.loli.net/2020/05/09/lzCanmhMr7FTYjo.png)
如果没有请在Terminal终端输入以下命令完成安装
```
pip install django
pip install mysqlclient
```
![](https://i.loli.net/2020/05/09/wzVQJoELaGk2ps5.png)
如果在执行pip install 报错Read time out请设置延长下超时时间,默认15s,网络不好情况下很易超时
```
pip --default-timeout=180 install -U django
pip --default-timeout=180 install -U mysqlclient
```
参数-U是--upgrade简写,把安装的包升级到最新版本
### 2. 创建应用
打开Pycharm的Terminal终端,输入以下命令创建sims应用
```
python manage.py startapp sims
```
应用创建后要在项目的settings.py文件里的INSTALLED_APPS下面添加smis完成应用注册
![](https://i.loli.net/2020/05/10/46nILBrYp7ATaGD.png)
### 3. 配置MySQL数据库
在本地MySQL创建sms数据库,修改项目的settings连接信息由默认的sqlite修改为MySQL
![](https://i.loli.net/2020/05/09/Hq39ULKg6BTsF1z.png)
``` python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'sms',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
```
测试连接,依次点击Pycharm右上角的Database->+->Data Source->MySQL
![](https://i.loli.net/2020/05/09/f6Nz8ZVlI1QMLU3.png)
下载连接驱动和配置数据库连接信息
![](https://i.loli.net/2020/05/09/c2vd637Te1Mfwit.png)
点击Test Connection测试连接,连接通过点击OK出现如下的结构信息表示连接本地MySQL成功
![](https://i.loli.net/2020/05/09/Y5fysXl14noZCGg.png)
### 4.数据模型创建(M)
在应用sims下models.py添加Student模型
![](https://i.loli.net/2020/05/10/P18qjX63nO2hedB.png)
``` python
class Student(models.Model):
student_no = models.CharField(max_length=32, unique=True)
student_name = models.CharField(max_length=32)
```
### 5.数据模型迁移
Terminal终端输入以下两条命令,其作用第一条生成文件记录模型的变化;第二条是将模型变化同步至数据库,我们可以在数据库生成对应的表结构。
python manage.py makemigrations sims
python manage.py migrate sims
生成数据表结构如下所示
![](https://i.loli.net/2020/05/10/RckQl4xBdz6MjK2.png)
### 6.路由配置
本质可以理解请求路径url和处理方法的映射配置,首先在项目sms的urls.py文件中添加sims的路由配置
``` python
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^sims/', include('sims.urls'))
]
```
然后在sims添加一个名为urls.py的文件,添加路由配置如下
![](https://i.loli.net/2020/05/10/VUarMbGES1up2dY.png)
``` python
# coding=utf-8
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^add/$', views.add),
url(r'^edit/$', views.edit),
url(r'^delete/$', views.delete)
]
```
### 7.处理函数添加(V)
在应用sims的视图层文件views.py添加对应学生信息增删改查的处理函数,这里我使用的原生SQL,便于深入理解其执行过程。后面有时间我会在github上添加Django框架提供的操作数据库方式。
``` python
import MySQLdb
from django.shortcuts import render, redirect
# Create your views here.
# 学生信息列表处理函数
def index(request):
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')
with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
cursor.execute("SELECT id,student_no,student_name FROM sims_student")
students = cursor.fetchall()
return render(request, 'student/index.html', {'students': students})
# 学生信息新增处理函数
def add(request):
if request.method == 'GET':
return render(request, 'student/add.html')
else:
student_no = request.POST.get('student_no', '')
student_name = request.POST.get('student_name', '')
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')
with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
cursor.execute("INSERT INTO sims_student (student_no,student_name) "
"values (%s,%s)", [student_no, student_name])
conn.commit()
return redirect('../')
# 学生信息修改处理函数
def edit(request):
if request.method == 'GET':
id = request.GET.get("id")
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')
with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
cursor.execute("SELECT id,student_no,student_name FROM sims_student where id =%s", [id])
student = cursor.fetchone()
return render(request, 'student/edit.html', {'student': student})
else:
id = request.POST.get("id")
student_no = request.POST.get('student_no', '')
student_name = request.POST.get('student_name', '')
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')
with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
cursor.execute("UPDATE sims_student set student_no=%s,student_name=%s where id =%s",
[student_no, student_name, id])
conn.commit()
return redirect('../')
# 学生信息删除处理函数
def delete(request):
id = request.GET.get("id")
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')
with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
cursor.execute("DELETE FROM sims_student WHERE id =%s", [id])
conn.commit()
return redirect('../')
```
### 8.模板页面创建(T)
![](https://i.loli.net/2020/05/10/XGdkWxvyTKRO7Ai.png)
- **学生信息列表页**
``` html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
<table border="1px" width="100%" style="border-collapse: collapse;">
<a href="../sims/add">添加学生</a>
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>操作</th>
</tr>
{% for student in students %}
<tr>
<td align="center">{{ forloop.counter }} </td>
<td align="center">{{ student.student_name }} </td>
<td align="center">{{ student.student_no }} </td>
<td align="center">
<a href="../sims/edit/?id={{ student.id }}">
编辑
</a>
<a href="../sims/delete/?id={{ student.id }}">
删除
</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
```
- **学生信息新增页**
``` html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生添加</title>
<style>
form {
margin: 20px auto;
width: 500px;
border: 1px solid #ccc;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本篇使用Python Web框架Django连接和操作MySQL数据库学生信息管理系统(SMS),主要包含对学生信息增删改查功能,旨在快速入门Python Web,少走弯路。 开发环境 开发工具:Pycharm 2020.1 开发语言:Python 3.8.0 Web框架:Django 3.0.6 数据库:MySQL5.7 操作系统:Windows 10
资源推荐
资源详情
资源评论
收起资源包目录
python-diango-web-master.zip (48个子文件)
python-diango-web-master
templates
student
index.html 1KB
add.html 710B
edit.html 842B
.idea
dataSources
d6d92fa2-1c5d-4538-8bfe-e03152caf6be.xml 4KB
d6d92fa2-1c5d-4538-8bfe-e03152caf6be
storage_v2
_src_
schema
performance_schema.kIw0nw.meta 76B
information_schema.FNRwLQ.meta 76B
mysql.osA4Bg.meta 63B
05a4fe04-3f24-49af-bd35-50b83f0fcba0
storage_v2
_src_
schema
performance_schema.kIw0nw.meta 76B
information_schema.FNRwLQ.meta 76B
mysql.osA4Bg.meta 63B
05a4fe04-3f24-49af-bd35-50b83f0fcba0.xml 4KB
vcs.xml 180B
misc.xml 294B
dataSources.xml 464B
inspectionProfiles
profiles_settings.xml 174B
modules.xml 258B
.gitignore 176B
sms.iml 1KB
sims
__init__.py 0B
tests.py 60B
admin.py 63B
migrations
__init__.py 0B
__pycache__
__init__.cpython-38.pyc 141B
apps.py 83B
models.py 200B
urls.py 217B
__pycache__
models.cpython-38.pyc 438B
urls.cpython-38.pyc 330B
admin.cpython-38.pyc 171B
__init__.cpython-37.pyc 146B
apps.cpython-37.pyc 358B
__init__.cpython-38.pyc 130B
views.cpython-38.pyc 2KB
views.py 3KB
manage.py 623B
.gitignore 2KB
sms
__init__.py 0B
wsgi.py 383B
urls.py 828B
settings.py 3KB
__pycache__
wsgi.cpython-38.pyc 524B
urls.cpython-38.pyc 997B
settings.cpython-38.pyc 2KB
settings.cpython-37.pyc 2KB
__init__.cpython-37.pyc 145B
__init__.cpython-38.pyc 129B
asgi.py 383B
README.md 10KB
共 48 条
- 1
资源评论
小蜜蜂vs码农
- 粉丝: 1614
- 资源: 149
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功