Combined use of Django framework + MySQL database (configure the mysql database in Django and implement the addition, deletion, modification and query operations of page data)

Combined use of Django framework + MySQL database (configure the mysql database in the Django framework and implement the addition, deletion, modification and query operations of page data)

Table of contents

        1. Premise:

        ​ ​ ​ 2. Combined use of Django framework + MySQL database

        ​ ​ 0. Install the third-party module mysqlclient

        ​ ​ 1. Create database

        ​ ​ 1) Start the MySQL service

         ​ ​ ​ 2) Bring your own tools to create a database

        ​ ​ 2. Configuration of Django connecting to mysql database

        3. Django action sheet

        ​ ​ 1) Create table

         ​ ​ ​ 2) Add data to the table

        ​ ​ 4. Write the [Display Data List] page:

        ​ ​ 5. Write the [Add Data] page:

        ​ ​ 6. Write the [Delete Data] page:

        ​ ​ 7. Write the [Modify Data] page:


1. Premise:

2. Combined use of Django framework + MySQL database

0. Install the third-party module mysqlclient

Install mysqlclient and run the following command on the command line:

pip install mysqlclient

But an error will be reported and the installation will not be successful. Solution: Download the wheel package of mysqlclient, then cd to the download and save directory of the wheel package, and install it offline. (See blog: python: Failed to install mysqlclient under windows - solution )

1. Create database

1) Start the MySQL service

mysql -u root -p    # 启动mysql服务
show databases;     # 显示数据库里的原有表格

 2) Bring your own tools to create a database

Create a database named packet_database:

create database packet_database DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

2. Configuration of Django connection to mysql database

Configure and modify in the setting.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':'packet_database', #数据库名字:刚才在mysql自行创建的那个数据库
        'USER': 'root',           #用户名
        'PASSWORD': 'root123',    #密码:自己在安装mysql时设置的进入密码
        'HOST': '127.0.0.1',      #哪台机器安装了MySQL,本机为127.0.0.1
        'PORT': '3306',           #端口号
    }
}

    

3. Django action sheets

1) Create table

Create a table: In the class of models.py, directly add the corresponding class, and then execute two commands: python manage.py makemigrations, python manage.py migrate.

  (1) In the models.py file, add the class:

from django.db import models

# Create your models here.
class PacketInfo(models.Model):   #在mysql数据库里,创建一个表格(名字为PacketInfo)
    name = models.CharField(max_length=64)  #表格里的其中一个字段为name(流量名称)
    type = models.CharField(max_length=64)    #表格里的其中一个字段为type(流量类型)
    desc = models.CharField(max_length=64)    #表格里的其中一个字段为desc(流量描述)

       

  (2) Execute commands: Execute the following two commands in the project root directory to create the table successfully.

python manage.py makemigrations
python manage.py migrate

      

  (3) Run show tables in mysql and you can see the table you just created.

use packet_database; //Use packet_database database
show tables; //View the tables in the database
desc show_packetinfo; //View the fields of the table

 2) Add data to the table

Write the following create statement in the .py file (as long as you run the project once, these sentences will be executed and a few rows of data will be added to the table. After adding enough, just comment out these sentences. Of course, you can also Use commands directly in mysql to add data.):

PacketInfo.objects.create(name="流量1",type="正常流量",desc="测试:正常流量1")  #往表里添加一行数据
PacketInfo.objects.create(name="流量2",type="正常流量",desc="测试:正常流量2")
PacketInfo.objects.create(name="流量3",type="正常流量",desc="测试:正常流量3")

# #### 1.新建数据 ########
# PacketInfo.objects.create(name="流量1")
# 相当于在mysql里: insert into show_packetinfo(name)values("流量1")

###### 2.删除数据 ########
# PacketInfo.objects.filter(id=3).delete() #把id=3的那一行删掉
# PacketInfo.objects.all().delete()   #把PacketInfo整张表删掉

###### 3.获取数据 ########
# data_list = [行,行,行]  每一行都是一个对象   QuerySet对象
# data_list = PacketInfo.objects.all() #all获取所有的数据,可以看作是一个列表
# for obj in data_list:
#     print(obj.id, obj,name, obj.password, obj.age)
#
# data_list = PacketInfo.objects.filter(id=1)  寻找id=1的那一行数据
# data_list = PacketInfo.objects.filter(id=1).first()  把这个queryset的第一个对象拿出来

The following addition, deletion, modification and search operations are from video screenshots (the ideas and operations are relatively simple and easy to understand, suitable for beginners to understand, learn and learn from). My own code is more complicated than this and is not easy to display directly.

4. Write the [Display Data List] page:

  • url(urls.py
  • Function (views.py)
    • Get all user information from the database in the background and return it to the front page.
    • HTML rendering, displays all information on the html page.

 

5. Write the [Add Data] page:

  • url(urls.py
  • Function (views.py)
    • GET, see the add page, enter the content.
    • POST, submit the content of the data → Obtain the data submitted by the user in the background and write it to the database.

 

Add an "Add" button to the list page so that you can jump to the "Add" page:

 

6. Write the [Delete Data] page:

  • url(urls.py
  • Function (views.py)
    • GET, click the delete button on the front page, get the id in the background, and then delete the id row.
    • After deletion, it will automatically jump to the "Display Data List" interface.

 

7. Write the [Modify Data] page:

  • url(urls.py
  • Function (views.py)
    • GET, after clicking the edit button on the front page, get the id in the background (get the id of the row of data to be edited), query this row of data in the database based on the id, and return the original data to the front desk, on the modification page display.
    • POST, submit the modified data on the editing page → obtain the data submitted by the user in the background, find the row of data in the database according to the id and update it. After updating the data, it will automatically jump to the "Display Data List" interface.

 Get the previous value and display:

 After submitting the form on the edit page, get the newly submitted value and update the record:

 

 

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/125246392