问题描述

笔者今天在运行Django项目时,发生报错如下:

File "E:\Python\python-3.7.3-amd64\lib\site-packages\django\db\backends\mysql\base.py", line 36, in <module>
    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.

Django同步数据时发生的问题。

解决方案

第一步

首先点击Terminal中的报错位置,比如我的是:

"E:\Python\python-3.7.3-amd64\lib\site-packages\django\db\backends\mysql\base.py"

找到其中的这两行代码,把这两行注释掉,作用是去除报错。

version = Database.version_info
# if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

第二步

再次运行,python manage.py runserver 0.0.0.0:8020报错如下:

File "E:\Python\python-3.7.3-amd64\lib\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'

首先还是点击Terminal中的报错位置,然后找到代码中的query.decode,将其改成query.encode:

def last_executed_query(self, cursor, sql, params):
     # With MySQLdb, cursor objects have an (undocumented) "_executed"
     # attribute where the exact query sent to the database is saved.
     # See MySQLdb/cursors.py in the source distribution.
     query = getattr(cursor, '_executed', None)
     if query is not None:
         query = query.encode(errors='replace')   # <--修改这里
     return query

Over,然后再次执行python manage.py runserver 0.0.0.0:8020就可以正常启动项目啦!


软件测试工程师一只,也在不断的学习阶段,平时的小经验不定期分享。
博主经验有限,若有不足,欢迎交流,共同改进~
有意可加Q群 908417285 交流学习。
乾坤未定,你我皆是黑马