3

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

Piero Pajares
  • 276
  • 1
  • 4
  • 21
  • Have you set the TZ environment variable in your WSGI file? If you're setting it in your .bashrc to make it use a particular timezone in consoles, you'll need to use `os.environ` to set it in the WSGI file too. – Giles Thomas Mar 24 '18 at 19:15
  • how? excuse my ignorance – Piero Pajares Mar 24 '18 at 19:28
  • If you have `export TZ=something` in the .bashrc, the equivalent for the WSGI file would be `import os` then `os.environ["TZ"] = "something"` – Giles Thomas Mar 25 '18 at 19:21
  • Ok add in my **console** : export TZ="/usr/share/zoneinfo/America/Lima" and in my **WSGI** add: os.environ["TZ"] = "America/Lima". After 12 a.m. I will check if it is solved. Thanks!!! :D – Piero Pajares Mar 25 '18 at 20:16
  • Yup, that should do the trick :-) – Giles Thomas Mar 26 '18 at 16:28
  • It did not work for me :( Update my question with the changes I made. – Piero Pajares Mar 27 '18 at 12:42
  • It's strange because before I used datetime instead of timezone and it did not work because after 12 a. M. The sales of the previous day kept appearing and in the same way it was solved when clicking on the Reload button. this person had the same problem, but he solved it with javascript, but it's not my case: https://www.pythonanywhere.com/forums/topic/9285/ – Piero Pajares Mar 27 '18 at 12:52
  • Ah, I think I see the problem. Your code in your view class is being executed when you reload the website, so `VentaToday.today` is always set to the time you last reloaded it. Instead of defining a variable called `today`, try defining `queryset` as `Venta.objects.filter(fecha=lambda: datetime.now(pytz.timezone('America/Lima'))).order_by('-id')` – Giles Thomas Mar 29 '18 at 21:19
  • this did not work for me :/ when I use lambda it shows me the following error: expected string or bytes-like object. I currently use Python 3.6 and django 1.9.6 – Piero Pajares Mar 30 '18 at 13:20
  • OK. Maybe try overriding the `get_queryset` method in your `VentaToday` class to return `Venta.objects.filter(fecha=datetime.now(pytz.timezone('America/Lima'))).order_by('-id')`...? – Giles Thomas Mar 30 '18 at 16:12
  • `class VentaToday(ListView): def get_queryset(self): return Venta.objects.filter(fecha=datetime.now(pytz.timezone('America/Lima'))).order_by('-id') template_name = 'venta/venta_today.html' ` In this way? Well, let's wait 12 to see if it updates haha – Piero Pajares Mar 30 '18 at 17:01
  • It worked! Thank you very much: D – Piero Pajares Mar 31 '18 at 13:46
  • Hooray! Glad to hear it :-D – Giles Thomas Apr 01 '18 at 20:08

2 Answers2

1

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')
Piero Pajares
  • 276
  • 1
  • 4
  • 21
1

TLDR: I had the same issue. I fixed it by changing TIME_ZONE='' to TIME_ZONE='UTC', in the settings.py file in project folder of pythonanywhere.

Python by default uses pytz.timezone(settings.TIME_ZONE), to initiate the time zone of the webapp, and since by default pythonanywhere doesnt initiate this variable, leaving it to the end user to do it, as per their requirements. So initiate your TIME_ZONE, as per your needs, which may do the trick.

You could also try looking in your project log files, for more information on this.

muditrustagii
  • 743
  • 6
  • 17
  • Only solution that worked for me. I tried editing both `.bashrc` and `wsgi.py` as [the tutorial](https://help.pythonanywhere.com/pages/SettingTheTimezone/) suggests, but it didn't work. It remained on UTC whatsoever. Changing `TIME_ZONE='UTC'` to `TIME_ZONE='America/New_York'` in `settings.py` did the trick – Alaa M. May 25 '20 at 13:31