The default configuration

The connection configuration information for the database is stored in settings.py, and Django uses the SQLite database for its default initial configuration.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3'.'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),}}Copy the code

To use the MySQL database, you first need to install the driver

pip install PyMySQL
Copy the code

Add the following statement to the __init__.py file in Django’s project subdirectory of the same name

import pymysql

pymysql.install_as_MySQLdb()
Copy the code

Djangos ORM allows PyMySQL to be called as mysqlDB.

Modify the configurations of DATABASES

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql'.'HOST': '127.0.0.1'.# database host
        'PORT': 3306.# database port
        'USER': 'root'.Database user name
        'PASSWORD': 'mysql'.Database user password
        'NAME': 'book'  # database name}}Copy the code