gpt4 book ai didi

django - 时区 它在本地工作,但在 pythonanywhere (DJango) 中不起作用

转载 作者:行者123 更新时间:2023-12-04 16:04:06 25 4
gpt4 key购买 nike

我有一个查询集来列出今天的销售额

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 点之后,昨天的销售情况一直出现,直到我单击 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

最佳答案

Giles Thomas 的解决方案:

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')

关于django - 时区 它在本地工作,但在 pythonanywhere (DJango) 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com