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

标签 django django-views django-timezone

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

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/

相关文章:

python - Django: activate() 没有显示效果

python - pytz 时区的 STD 偏移量

Django 和 Facebook : security & design for a facebook webapp that performs a third party login on behalf of the user

css - 为什么 Django 无法显示我的 CSS 文件中的图像?

javascript - 使用 ionic、Angular 和 django+rest 框架上传图像

python - Python Django-REST-framework 和 Angularjs 的文件夹结构

django - 请求对象没有用户 Django

django - 使用 DRF 中序列化器字段的子集反序列化 POST 请求

Django 注销按钮

python - DateTimeField 收到一个天真的日期时间