经过一段时间使用,目前的django相关编程软件更新如下:
python 3.8.0
django 3.0
mysql8.0.18
---------------------------------------------------------------------------
1、django 3.0 用pip的安装没有成功。下载 Django 压缩包,解压并和Python安装目录放在同一个根目录,
进入 Django 目录,执行python setup.py install,然后开始安装,Django将要被安装到Python的Lib下site-packages。
bin目录 为 C:\Python38\Lib\site-packages\Django-3.0-py3.8.egg\django\bin ,多了一层 Django-3.0-py3.8.egg 。
2、建立django项目后,更改 settings.py 内 的 LANGUAGE_CODE = 'zh-hans' , 框架变简体中文。
django2.x 里面是 LANGUAGE_CODE = 'zh-Hans'。
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
3、django3.0连接mysql8.0,起初安装了 pymysql,配置项目内 的 __init__.py,
import pymysql
pymysql.install_as_MySQLdb()
再配置settings.py 内 的数据库连接
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # or use: mysql.connector.django
'NAME': 'octopus',
'USER': 'root',
'PASSWORD': 'Aa123456',
'HOST': '127.0.0.1',
'PORT': 3306,
'OPTIONS': {
'autocommit': True,
},
}
}
结果,进行admin应用的数据库迁移时,发生版本识别错误
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
原因:在 C:\Python38\Lib\site-packages\Django-3.0-py3.8.egg\django\db\backends\mysql下的 base.py 中 有解释,看浅绿色的提示。
所以,直接为python安装 mysqlclient : pip install mysqlclient,最新的版本是 1.4.6
"""
MySQL database backend for Django.
Requires mysqlclient: https://pypi.org/project/mysqlclient/
"""
import re
from django.core.exceptions import ImproperlyConfigured
from django.db import utils
from django.db.backends import utils as backend_utils
from django.db.backends.base.base import BaseDatabaseWrapper
from django.utils.asyncio import async_unsafe
from django.utils.functional import cached_property
try:
import MySQLdb as Database
except ImportError as err:
raise ImproperlyConfigured(
'Error loading MySQLdb module.\n'
'Did you install mysqlclient?'
) from err
from MySQLdb.constants import CLIENT, FIELD_TYPE # isort:skip
from MySQLdb.converters import conversions # isort:skip
# Some of these import MySQLdb, so import them after checking if it's installed.
from .client import DatabaseClient # isort:skip
from .creation import DatabaseCreation # isort:skip
from .features import DatabaseFeatures # isort:skip
from .introspection import DatabaseIntrospection # isort:skip
from .operations import DatabaseOperations # isort:skip
from .schema import DatabaseSchemaEditor # isort:skip
from .validation import DatabaseValidation # isort:skip
version = Database.version_info
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
# MySQLdb returns TIME columns as timedelta -- they are more like timedelta in
# terms of actual behavior as they are signed and include days -- and Django
# expects time.
django_conversions = {
**conversions,
**{FIELD_TYPE.TIME: backend_utils.typecast_time},
}
4、连接mysql8.0,需要把 密码设置方式 改为老版的 mysql_native_password,暂时应该是不支持 caching_sha2_password
折腾了小半天,终于环境搞通了,记录一下,以后少走弯路。争取一年内不升级大版本了!