我有一个Django应用程序。我正在使用在docker容器中运行的MySql服务器作为数据库。刚刚移动到自定义用户模型后。现在我收到了这些错误: 环境:
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/
Django Version: 1.10.3
Python Version: 3.5.2
Installed Applications:
['grappelli',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.sites',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'oauth2_provider',
'social.apps.django_app.default',
'rest_framework_social_oauth2',
'rest_framework_swagger',
'accounts',
'dashboard',
'items',
'storages',
'userprofile',
'common',
'registration',
'debug_toolbar',
'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/kuba/code/VeeU/Server/dashboard/views.py" in index
66. Bookmark.objects.datetimes('date_bookmarked', 'day')]
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
256. self._fetch_all()
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
1087. self._result_cache = list(self.iterator())
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
155. for row in compiler.results_iter():
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in results_iter
795. row = self.apply_converters(row, converters)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in apply_converters
779. value = converter(value, expression, self.connection, self.query.context)
File "/home/kuba/.virtualenvs/VeeU/lib/python3.5/site-packages/django/db/models/functions/datetime.py" in convert_value
181. "Database returned an invalid datetime value. "
Exception Type: ValueError at /dashboard/
Exception Value: Database returned an invalid datetime value. Are time zone definitions for your database and pytz installed?
我试图运行mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
但它不起作用。我还尝试在容器中的default-time-zone = 'UTC'
中设置/etc/mysql/my.cnf
。
编辑:
我也安装了pytz
pip freeze | grep pytz
pytz==2016.7
答案 0 :(得分:2)
我在Debian Linux上遇到与django和mysql相同的问题并且能够通过运行此命令解决它,该命令填充名为 mysql <的(config)数据库中的 time_zone 表/强>:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
答案 1 :(得分:0)
MySQL数据库包含似乎缺少的时区信息表。您可以使用以下方法重新填充数据库:
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | mysql -u root mysql
然后
mysql -u root -p -e "flush tables;" mysql
答案 2 :(得分:0)
我看到你的 django 项目有问题,所以让我们在 django 中修复它。 只需更改settings.py 来自
USE_TZ = True
到
USE_TZ = False
无论如何,它对我有用!