在/project/settings.py文件中修改数据库配置。

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

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'NAME': 'guest',
        'USER': 'root',
        'PASSWORD': '123456',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}
File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in 
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

解决办法:

Python36libsite-packagesdjangodbbackendsmysqlbase.py下注释掉以下代码

if version 

执行 python3 manage.py migrate,如下报错

  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'

解决办法:

找到错误代码:query = query.encode(errors=’replace’)

解决方法:把decode改为encode即可。
再执行python3 manage.py migrate

文章来源于互联网,如有雷同请联系站长删除:解决在 Django 中配置 MySQL 报错问题

发表评论