时区 它在本地工作,但在 pythonanywhere (DJango) 中不起作用
Timezone It works locally but not in pythonanywhere (DJango)
我有一个 查询集 来列出今天的销售额
from django.utils import timezone
class VentaToday(ListView):
queryset = Venta.objects.filter(fecha=timezone.now()).order_by('-id')
template_name = 'venta/venta_today.html'
在本地,这可以正常工作,但在生产环境 (Pythonanywhere) 中,前一天的销售额不断出现。要修复它,我必须转到 pythonanywhere 面板并单击 ** reload ** 按钮来解决问题。
我更改了服务器时间:
Image of server time
django项目的配置:
LANGUAGE_CODE = 'es-pe'
TIME_ZONE = 'America/Lima'
USE_I18N = True
USE_L10N = True
USE_TZ = True
是不是服务器缓存问题?或者我做错了什么?
更新
配置 WSGI:
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
os.environ["TZ"] = "America/Lima"
#
## assuming your django settings file is at '/home/dnicosventas/mysite/mysite/settings.py'
## and your manage.py is is at '/home/dnicosventas/mysite/manage.py'
path = '/home/dnicosventas/dnicos-ventas'
if path not in sys.path:
sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'DnicosVentas.settings'
#
## then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
## or, for older django <=1.4
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
和我的控制台:
export TZ="/usr/share/zoneinfo/America/Lima"
即便如此,在 12 a.m. 之后,昨天的销售一直出现,直到我在 pythonanywhere 面板中单击重新加载按钮。
Views.py:
class VentaToday(ListView):
today = datetime.now(pytz.timezone('America/Lima'))
queryset = Venta.objects.filter(fecha=today).order_by('-id')
template_name = 'venta/venta_today.html'
Image of the reload button
贾尔斯·托马斯的解决方案:
class VentaToday(ListView):
template_name = 'venta/venta_today.html'
def get_queryset(self):
return Venta.objects.filter(fecha=datetime.now(pytz.timezone('America/Lima'))).order_by('-id')
TLDR:我遇到了同样的问题。我通过在 pythonanywhere 的项目文件夹中的 settings.py 文件中将 TIME_ZONE=''
更改为 TIME_ZONE='UTC'
来修复它。
Python 默认使用 pytz.timezone(settings.TIME_ZONE)
来启动 webapp 的时区,因为默认情况下 pythonanywhere 不会启动这个变量,让最终用户去做,按照他们的要求。因此,根据您的需要启动您的 TIME_ZONE,这可能会成功。
您也可以尝试查看您的项目日志文件,以获取更多相关信息。
我有一个 查询集 来列出今天的销售额
from django.utils import timezone
class VentaToday(ListView):
queryset = Venta.objects.filter(fecha=timezone.now()).order_by('-id')
template_name = 'venta/venta_today.html'
在本地,这可以正常工作,但在生产环境 (Pythonanywhere) 中,前一天的销售额不断出现。要修复它,我必须转到 pythonanywhere 面板并单击 ** reload ** 按钮来解决问题。
我更改了服务器时间:
Image of server time
django项目的配置:
LANGUAGE_CODE = 'es-pe'
TIME_ZONE = 'America/Lima'
USE_I18N = True
USE_L10N = True
USE_TZ = True
是不是服务器缓存问题?或者我做错了什么?
更新 配置 WSGI:
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
os.environ["TZ"] = "America/Lima"
#
## assuming your django settings file is at '/home/dnicosventas/mysite/mysite/settings.py'
## and your manage.py is is at '/home/dnicosventas/mysite/manage.py'
path = '/home/dnicosventas/dnicos-ventas'
if path not in sys.path:
sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'DnicosVentas.settings'
#
## then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
## or, for older django <=1.4
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
和我的控制台:
export TZ="/usr/share/zoneinfo/America/Lima"
即便如此,在 12 a.m. 之后,昨天的销售一直出现,直到我在 pythonanywhere 面板中单击重新加载按钮。
Views.py:
class VentaToday(ListView):
today = datetime.now(pytz.timezone('America/Lima'))
queryset = Venta.objects.filter(fecha=today).order_by('-id')
template_name = 'venta/venta_today.html'
Image of the reload button
贾尔斯·托马斯的解决方案:
class VentaToday(ListView):
template_name = 'venta/venta_today.html'
def get_queryset(self):
return Venta.objects.filter(fecha=datetime.now(pytz.timezone('America/Lima'))).order_by('-id')
TLDR:我遇到了同样的问题。我通过在 pythonanywhere 的项目文件夹中的 settings.py 文件中将 TIME_ZONE=''
更改为 TIME_ZONE='UTC'
来修复它。
Python 默认使用 pytz.timezone(settings.TIME_ZONE)
来启动 webapp 的时区,因为默认情况下 pythonanywhere 不会启动这个变量,让最终用户去做,按照他们的要求。因此,根据您的需要启动您的 TIME_ZONE,这可能会成功。
您也可以尝试查看您的项目日志文件,以获取更多相关信息。