Python Django框架下将MySQL数据库的内容在网页上动态展示(修订版-2021-05-17)

简介: Python Django框架下将MySQL数据库的内容在网页上动态展示(修订版-2021-05-17)

    Python Django框架下将MySQL数据库的内容在网页上动态展示(修订版-2021-05-17)

需求:将数据库中的一个库内的单表数据展示在网页首页,该表有id列,名字列,学号列三个列,并有简单的逻辑:能修改单行内的名字和学号,能删除任意的行,修改和删除后仍然自动跳转到展示页。没有分页,权限管理等功能,仅仅是个简单的展示、

 

 

删除后:

修改中:

 

============================================================================

下面就开始讲解:

一,

项目的建立

首先,新建项目,项目名称为sims。运行命令:Python manage.py  startproject  sims

这里需要说明一下环境,使用的是Python3.8,pycharm专业版,使用Windows,所建立的项目,Django的版本等等见下面(Django使用的是1.11.27,我安装了一个jupyter):

argon2-cffi==20.1.0
async-generator==1.10
attrs==21.2.0
backcall==0.2.0
bleach==3.3.0
cffi==1.14.5
colorama==0.4.4
decorator==5.0.9
defusedxml==0.7.1
Django==1.11.27
entrypoints==0.3
ipykernel==5.5.5
ipython==7.23.1
ipython-genutils==0.2.0
ipywidgets==7.6.3
jedi==0.18.0
Jinja2==3.0.0
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==6.2.0
jupyter-console==6.4.0
jupyter-core==4.7.1
jupyterlab-pygments==0.1.2
jupyterlab-widgets==1.0.0
MarkupSafe==2.0.0
matplotlib-inline==0.1.2
mistune==0.8.4
mysqlclient==1.4.6
nbclient==0.5.3
nbconvert==6.0.7
nbformat==5.1.3
nest-asyncio==1.5.1
notebook==6.3.0
packaging==20.9
pandocfilters==1.4.3
parso==0.8.2
pickleshare==0.7.5
prometheus-client==0.10.1
prompt-toolkit==3.0.18
pycparser==2.20
pyftpdlib==1.5.6
Pygments==2.9.0
pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pytz==2021.1
pywin32==300
pywinpty==0.5.7
pyzmq==22.0.3
qtconsole==5.1.0
QtPy==1.9.0
Send2Trash==1.5.0
six==1.16.0
terminado==0.9.5
testpath==0.4.4
tornado==6.1
traitlets==5.0.5
wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.5.1

数据库使用的是mariadb-5.5,安装方式为centos7.2下的RPM包安装 方式,这里需要注意,因为是RPM包安装,因此,需要对mariadb做适当的修改,主要是字符集的设置,这个就不在此讨论了,因为这些是基本的运维工作。

整体项目使用的是mariadb驱动,具体版本为mariadb-java-client-2.6.0.jar

在项目目录下的settings.py文件中注册app。app名称为test(我用user,这个APP不能注册,原因未知,很郁闷)。以下为setting的全部内容(不包括注释)

需要修改的地方是:

INSTALLED_APPS
TEMPLATES
DATABASES

这里需要说明一下,DATABASES这一块是使用的是mysql,mariadb和mysql  Django认为没有什么太大的区别。

import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'k&p=zqkw7(vls^6avyjjv-_ro11e93y#8w-xgp9d1qs$!ejf0w'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'test',
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'sims.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
WSGI_APPLICATION = 'sims.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
    'default': {
#使用的驱动引擎是mysql
        'ENGINE': 'django.db.backends.mysql',
#这个name是数据库的库名哦,一会建库的时候可要一致哦
        'NAME': 'mysims',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '192.168.88.11',
        'PORT': '3306',
    }
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'

 

 

注意,数据库最好先建立,模板文件夹是在工程的下一级目录,右键点击项目名称为sims那个文件夹,新建文件夹,名称为templates。右键点击templates文件夹,再建立一个文件夹,名称为student。以上准备完毕后,运行命令:python manage.py startapp  user(注册的哪个名字就写哪个名字)。至此,基本的项目环境准备完毕(Django项目和python所使用的MySQL连接驱动要安装哦,否则,初始化MySQL数据库不会成功)。

============================================================================

运行命令:Python manage.py  startapp user

执行完毕后,在项目下会多出一个平级目录并且该目录为前面注册的APP名称,其下有五个文件。整体项目的目录大概是长这样的~~~~

二,

数据库的初始化工作

接下来开始编辑 models.py文件

from django.db import models
# Create your models here.
class Student(models.Model):
    student_no = models.CharField(max_length=32,unique=True)
    student_name = models.CharField(max_length=32)
    student_city = models.CharField(max_length=32)

这个类名就是将要新建立的表的名称,最终数据中生成的表的名称为app名称_类名,所以请注意这个类名,尽量的有些含义。

执行两个命令:python manage.py  makemigrations test

python manage.py migrate test

执行完毕后,数据库中所使用的表将会初始化并生成。当然,表暂时是空的,里面没有数据(这是原始的第一版图,不要写user哦,要写test!!!!!)

此时,我们登录mariadb,可以观察到:

MariaDB [(none)]> use mysims;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysims]> show tables;
+----------------------------+
| Tables_in_mysims           |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
| test_student               |
+----------------------------+
11 rows in set (0.00 sec)

很明显的,我们这个数据库自动生成了很多表,其中主要的是test_student 这个表,该表将是我们这个项目所使用的主要表,学生的学号和学生的姓名等都是持久化存储到这个表里的这个表的名字是APP的名称_models.py这个文件所定义的类。(一定要深刻理解这个数据库中表的名字的由来)

这样的就是成功初始化了。

===========================================================================

三,

配置路由

test/urls.py文件:

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),
                ]

 

sims/urls.py:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('test.urls')),
]

============================================================================

四,

配置视图(逻辑函数都在里面)

test/views.py

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
import MySQLdb
from django.shortcuts import render, redirect # Create your views here.
#学生信息列表处理函数,直接读取数据库的内容,将内容渲染到静态展示页面,算是查这个功能的实现。
def index(request):
    conn = MySQLdb.connect(host="192.168.88.11",
    user="root",
    passwd="123456",
    db="mysims",
    charset='utf8')
    with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
        cursor.execute("SELECT id,student_no,student_name,student_city FROM test_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', '')
        student_city = request.POST.get('student_city')
        conn = MySQLdb.connect(host="192.168.88.11", user="root", passwd="123456", db="mysims", charset='utf8')
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("INSERT INTO test_student (student_no,student_name,student_city) " "values (%s,%s,%s)", [student_no, student_name,student_city])
    conn.commit()
    return redirect('../')
# 学生信息修改处理函数,同上,只是sql语句不一样而已,换成了update语句。
def edit(request):
    if request.method == 'GET':
        id = request.GET.get("id")
        conn = MySQLdb.connect(host="192.168.88.11", user="root", passwd="123456", db="mysims", charset='utf8')
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("SELECT id,student_no,student_name,student_city FROM test_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', '')
        student_city = request.POST.get('student_city', '')
        conn = MySQLdb.connect(host="192.168.88.11", user="root", passwd="123456", db="mysims", charset='utf8')
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("UPDATE test_student set student_no=%s,student_name=%s,student_city=%s where id =%s", [student_no, student_name, student_city, id])
    conn.commit()
    return redirect('../')
# 学生信息删除处理函数,同上,SQL语句改变而已。
def delete(request):
    id = request.GET.get("id")
    conn = MySQLdb.connect(host="192.168.88.11", user="root", passwd="123456", db="mysims", charset='utf8')
    with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
        cursor.execute("DELETE FROM test_student WHERE id =%s", [id])
    conn.commit()
    return redirect('../')
#返回上层url

==============================================================================

五,

前端页面的生成

三个HTML文件:

template/student/.index.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="../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="../edit/?id={{ student.id }}"> 编辑 </a>
            <a href="../delete/?id={{ student.id }}"> 删除 </a>
        </td> </tr> {% endfor %}
</table>
</body>
</html>

 

template/student/add.html:

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
    <title>学生添加</title>
    <style>
     form { margin: 20px auto; width: 500px; border: 1px solid #ccc; padding: 20px }
</style>
</head>
<body> <form method="post" action="../add/"> {% csrf_token %}
    <table> <tr>
        <th>姓名</th> <td><input name="student_name"></td>
    </tr>
        <tr> <th>学号</th>
            <td><input name="student_no"/></td>
        </tr>
        tr> <th>城市</th>
            <td><input name="student_city"/></td>
        </tr>
        <tr>
            <td colspan="3">
            <input type="submit"/> </td>
        </tr>
    </table>
</form>
</body>
</html>

template/student/edit.html:

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
    <title>学生编辑</title>
    <style> form { margin: 20px auto; width: 500px; border: 1px solid #ccc; padding: 20px }
</style>
</head> <body>
<form method="post" action="../edit/"> {% csrf_token %}
    <input type="hidden" name="id" value="{{ student.id }}"/>
    <table> <tr> <th>姓名</th> <td><input name="student_name" value="{{ student.student_name }}"></td></tr>
        <tr> <th>学号</th> <td><input name="student_no" value="{{ student.student_no }}"/></td> </tr>
        <tr> <th>城市</th> <td><input name="student_city" value="{{ student.student_city }}"></td></tr><tr>
        <td colspan="2"> <input type="submit"/> </td>
    </tr>
    </table>
</form>
</body>
</html>

因为删除数据后就直接跳转到查看页面了,因此不需要单独编写,只需要前后端数据同步好后,刷新index页面,能在展示页面也就是index.html这看到就可以了。增加和修改功能是另外的逻辑,因此需要单独编写。

 

这个简单项目的运行

那么,现在就可以运行命令:Python manage.py runserver,

该程序会启动开发服务器,以便使用浏览器来测试你的APP运行。

 

总结:

严格按照本博客编写这个项目,是一定会写出这些页面的,其中涉及到的知识点比较多,前后端的知识点都有所涉猎,如果,对开发有兴趣,我想,从环境的搭建开始,是一个不错的开始。

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
10月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
1061 7
|
10月前
|
Java 数据处理 索引
(Pandas)Python做数据处理必选框架之一!(二):附带案例分析;刨析DataFrame结构和其属性;学会访问具体元素;判断元素是否存在;元素求和、求标准值、方差、去重、删除、排序...
DataFrame结构 每一列都属于Series类型,不同列之间数据类型可以不一样,但同一列的值类型必须一致。 DataFrame拥有一个总的 idx记录列,该列记录了每一行的索引 在DataFrame中,若列之间的元素个数不匹配,且使用Series填充时,在DataFrame里空值会显示为NaN;当列之间元素个数不匹配,并且不使用Series填充,会报错。在指定了index 属性显示情况下,会按照index的位置进行排序,默认是 [0,1,2,3,...] 从0索引开始正序排序行。
708 0
|
10月前
|
存储 Java 数据处理
(numpy)Python做数据处理必备框架!(一):认识numpy;从概念层面开始学习ndarray数组:形状、数组转置、数值范围、矩阵...
Numpy是什么? numpy是Python中科学计算的基础包。 它是一个Python库,提供多维数组对象、各种派生对象(例如掩码数组和矩阵)以及用于对数组进行快速操作的各种方法,包括数学、逻辑、形状操作、排序、选择、I/0 、离散傅里叶变换、基本线性代数、基本统计运算、随机模拟等等。 Numpy能做什么? numpy的部分功能如下: ndarray,一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环)。 用于读写磁盘数据的工具以及用于操作内存映射文件的工具。 线性代数、随机数生成以及傅里叶变换功能。 用于集成由C、C++
733 1
|
10月前
|
Java 数据挖掘 数据处理
(Pandas)Python做数据处理必选框架之一!(一):介绍Pandas中的两个数据结构;刨析Series:如何访问数据;数据去重、取众数、总和、标准差、方差、平均值等;判断缺失值、获取索引...
Pandas 是一个开源的数据分析和数据处理库,它是基于 Python 编程语言的。 Pandas 提供了易于使用的数据结构和数据分析工具,特别适用于处理结构化数据,如表格型数据(类似于Excel表格)。 Pandas 是数据科学和分析领域中常用的工具之一,它使得用户能够轻松地从各种数据源中导入数据,并对数据进行高效的操作和分析。 Pandas 主要引入了两种新的数据结构:Series 和 DataFrame。
767 0
|
10月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
441 0
|
11月前
|
数据采集 关系型数据库 MySQL
python爬取数据存入数据库
Python爬虫结合Scrapy与SQLAlchemy,实现高效数据采集并存入MySQL/PostgreSQL/SQLite。通过ORM映射、连接池优化与批量提交,支持百万级数据高速写入,具备良好的可扩展性与稳定性。
|
11月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
622 158
|
11月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
11月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1638 152
|
11月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1125 156

推荐镜像

更多