Django 服务器显示错误的时间

Posted

技术标签:

【中文标题】Django 服务器显示错误的时间【英文标题】:Django server shows wrong time 【发布时间】:2020-07-16 23:46:23 【问题描述】:

我的 django 服务器时间有问题,晚了 2 小时。 我试图找到一个解决方案,但我发现的任何东西(如How to change my django server time 或Django documentation)都不是我认为我需要的。

现在我的电脑时间是 23:14:37 当我写 bash date +%H:%M:%S 时,我会得到:

(python_env) bash-3.2$ date +%H:%M:%S
23:17:03

所以我不认为这是我的控制台问题。

但是当我运行我的服务器时,我得到的是:

(python_env) bash-3.2$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 04, 2020 - 21:18:47
Django version 2.2.4, using settings 'myVote365.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

现在时间晚了 2 小时。

我有红色可能是因为时区错误,但我的代码类似于How to change my django server time

LANGUAGE_CODE = 'pl'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

更重要的是,在我将操作系统更改为 Windows 并再次更改为 OS X 之前,它运行得非常好

编辑:

我将USE_TZ 更改为False,运行并关闭服务器,然后再次更改为True。 现在虽然我还有一个服务器迟到了 2 小时。但从错误:

Internal Server Error: /panel/
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 189, in _get_session
    return self._session_cache
AttributeError: 'SessionStore' object has no attribute '_session_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_session

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/icookiez/Documents/praca/dla taty/myVote365/myVote365/audytor/views.py", line 26, in panel
    if 'auditor' in request.session and request.session['auditor']['logged'] is True:
  File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 51, in __contains__
    return key in self._session
  File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 194, in _get_session
    self._session_cache = self.load()
  File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 43, in load
    s = self._get_session_from_db()
  File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 34, in _get_session_from_db
    expire_date__gt=timezone.now()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 402, in get
    num = len(clone)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_session

我有一个工作站点。

【问题讨论】:

【参考方案1】:

第一步

我将USE_TZ 更改为False,运行并关闭服务器,然后再次更改为True。这让我这边可以加载,但仍然有错误的时间。

第二步

我没有将 TIME_ZONE 更改为 @jatrp5 建议的“PL”,而是更改为“Europe/Warsaw”,正如我在 this question 上看到的那样。使用区域名称形式Wikipedia。现在我有时间了。

【讨论】:

【参考方案2】:

要匹配您的 bash 时间,您应该将时区更改为 TIME_ZONE = 'PL'。世界时区 (UTC) 比波兰语晚两小时。

【讨论】:

我有错误ValueError: Incorrect timezone setting: PL 我从几个月开始就有这种情况,这是第一次发生这种情况(波兰总是 UTC+1 或 UTC+2 取决于季节)

以上是关于Django 服务器显示错误的时间的主要内容,如果未能解决你的问题,请参考以下文章

Django Rest框架 - 如果表包含响应显示服务器错误(500)的数据

Django 开发服务器显示错误 61 Connection Refused with Redis

如何在 Cloud Run 的日志中显示 django 日志记录和错误日志?

django动态拉链内部服务器错误

Django 服务器错误:端口已在使用中

你如何在 django 网站上记录服务器错误