数据库返回了无效的日期时间值。是否安装了数据库和pytz的时区定义?

时间:2017-02-10 09:19:59

标签: django python-3.x

python版本:3.6

OS:windows 10

mysql版本:5.7

我有一个Django应用程序。现在我收到了这些错误:环境:

Environment:


Request Method: GET
Request URL: http://localhost:8000/blog/archive/

Django Version: 1.10.5
Python Version: 3.6.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bookmark.apps.BookmarkConfig',
 'blog.apps.BlogConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\dates.py" in get
  339.         self.date_list, self.object_list, extra_context =     self.get_dated_items()

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\dates.py" in get_dated_items
  425.         date_list = self.get_date_list(qs, ordering='DESC')

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\dates.py" in get_date_list
  404.         if date_list is not None and not date_list and not allow_empty:

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in __bool__
  260.         self._fetch_all()

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in __iter__
  155.         for row in compiler.results_iter():

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  795.                     row = self.apply_converters(row, converters)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in apply_converters
  779.                 value = converter(value, expression, self.connection, self.query.context)

File "C:\Users\ikks0\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\functions\datetime.py" in convert_value
  181.                         "Database returned an invalid datetime value. "

Exception Type: ValueError at /blog/archive/
Exception Value: Database returned an invalid datetime value. Are time zone definitions for your database and pytz installed?

我尝试了#34; MySQL mysql_tzinfo_to_sql program"。但这不起作用。

解决方案是什么?

1 个答案:

答案 0 :(得分:0)

from django.views.generic.list import ListView

from django.utils import timezone

...

class NewsListView(ListView, ArchiveIndexView, CategoryListMixin):

model = New

...

def get_context_data(self, **kwargs):

context = super(NewsListView, self).get_context_data(**kwargs) context['now'] = timezone.now() return context

...

https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-display/#listview