Search code examples
djangodjango-viewsdjango-timezone

Timezone It works locally but not in pythonanywhere (DJango)


I have a queryset to list today's sales

from django.utils import timezone

class VentaToday(ListView):
    queryset = Venta.objects.filter(fecha=timezone.now()).order_by('-id')
    template_name = 'venta/venta_today.html'

In local, this works correctly but in production (Pythonanywhere) the sales of the previous day keep appearing. To fix it, I have to go to the pythonanywhere panel and click on the ** reload ** button to solve the problem.

I changed the server time:

Image of server time

Configuration of the django project:

LANGUAGE_CODE = 'es-pe'

TIME_ZONE = 'America/Lima'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Is it a server cache problem? or something am I doing wrong?

UPDATE config 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()

and my console:

export TZ="/usr/share/zoneinfo/America/Lima"

Even so, after 12 a.m., yesterday's sales keep appearing until I click on the reload button in the pythonanywhere panel.

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


Solution

  • Solution by 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')