在修改了settings.py配置到连接mysql后,报了以下的错误
python manage.py migrate
Traceback (most recent call last):
File "D:\python\code\venv\lib\site-packages\django\db\backends\mysql\base.py", lin
e 15, in <module>
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\python\code\venv\lib\site-packages\django\core\management\__init__.py", l
ine 381, in execute_from_command_line
utility.execute()
File "D:\python\code\venv\lib\site-packages\django\core\management\__init__.py", l
ine 357, in execute
django.setup()
File "D:\python\code\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\python\code\venv\lib\site-packages\django\apps\registry.py", line 112, in
populate
app_config.import_models()
File "D:\python\code\venv\lib\site-packages\django\apps\config.py", line 198, in i
mport_models
self.models_module = import_module(models_module_name)
File "D:\python\code\venv\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "" , line 986, in _gcd_import
File "" , line 969, in _find_and_load
File "" , line 958, in _find_and_load_unlocked
File "" , line 673, in _load_unlocked
File "" , line 665, in exec_module
File "" , line 222, in _call_with_frames_removed
File "D:\python\code\venv\lib\site-packages\django\contrib\auth\models.py", line 2
, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "D:\python\code\venv\lib\site-packages\django\contrib\auth\base_user.py", lin
e 47, in <module>
class AbstractBaseUser(models.Model):
File "D:\python\code\venv\lib\site-packages\django\db\models\base.py", line 101, i
n __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "D:\python\code\venv\lib\site-packages\django\db\models\base.py", line 305, i
n add_to_class
value.contribute_to_class(cls, name)
File "D:\python\code\venv\lib\site-packages\django\db\models\options.py", line 203
, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "D:\python\code\venv\lib\site-packages\django\db\__init__.py", line 33, in __
getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "D:\python\code\venv\lib\site-packages\django\db\utils.py", line 202, in __ge
titem__
backend = load_backend(db['ENGINE'])
File "D:\python\code\venv\lib\site-packages\django\db\utils.py", line 110, in load
_backend
return import_module('%s.base' % backend_name)
File "D:\python\code\venv\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "D:\python\code\venv\lib\site-packages\django\db\backends\mysql\base.py", lin
e 20, in <module>
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
因为在python3.*中安装mysqldb会报错,所以需要先在cmd中执行以下命令先安装pymysql替代
pip install pymysql
在_init_.py中添加以下代码
import pymysql
pymysql.install_as_MySQLdb()
修改后,执行
python manage.py migrate
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
完毕。