Django2. * + mysql5.7 development environment integration tutorial diagram

Time:2020-12-4

Environmental Science:

MAC_OS 10.12

python 3.6

mysql 5.7.25

django 2.2.3

Premise: Python Django MySQL has been successfully installed and can be run independently

1、 Database configuration in settings


DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    "USER": "user name",
    "PASSWORD": "your password",
    "NAME": "db name"
  }
}

2、 Replace default database driver

1. Install pymysql

pip3 install pymysql

2. Replace the drive

Find the project main package. For example, the project is called test. Find the__ init__ . py file

Add the following code:


import pymysqlpymysql.install_as_MySQLdb()

3、 Problem solving:

Question 1: the version of mysqlclient


"""
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
"""

Solution: find the corresponding 35 and 36 lines according to the path in the figure and annotate them out;

Question 2: string encoding and decoding


"""
AttributeError: 'str' object has no attribute 'decode'
"""

Solution: click the exception tracking information to find operations.py In line 146, change code to encode

Test run:

PS: the reason for modifying the source code is the compatibility between django2 and python3 and pymysql. In addition, the reason for choosing 2. * is because of the official support time,

The default database driver of Django is mysqlclient, but it is found in the test that it can not run at all, and the import of MySQL link libraries fails

summary

The above is the Django 2. * + mysql5.7 development environment integration tutorial diagram, I hope to help you, if you have any questions, please leave me a message, the editor will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!