Datetime.now () in a Django application does not work well

I am having problems with the Django application after deploying it. I am using Apache + mod-wsgi on an ubuntu server. Some time after the server reboots, time goes back, this is incorrect after about 10 hours. I made a Django view that looks like this:

def servertime(): return HttpResponse( datetime.now() ) 

and after rebooting the server and checking the URL, which shows that it looks okay. Then at some point he sometimes gives the correct time, and sometimes not, and then always gives the wrong time. Server time is all the same.

Any clues? I was looking for him with no luck.

+6
python django datetime apache mod-wsgi
source share
7 answers

I found that using wsgi in daemon mode works. I don’t know why, but it was so. It seems that some of the newly created processes are wasting time.

+5
source share

Can i see your urls.py?

Such behavior has crushed me before ...

What turned out to be what my urls.py called the view. Python ran datetime.now () once and saved it for future calls, but never called it again. That is why django developers had to implement the ability to pass a function, rather than a function call, to the model's default value, as this would require the first function call and use it until python restarted.

Your behavior sounds like the first time, right, because it was first triggered. It was wrong from time to time because it got the same date again. Then this was accidentally correct, because your apache probably started another workflow for it, and the craziness is likely to happen when you get a bounce between which process was processing the request.

+6
source share

datetime.now () is probably evaluated once when your class is created. Try removing the parentheses to return the datetime.now and THEN functions. I had a similar problem with setting default values ​​for my DateTimeFields and wrote my solution here .

+2
source share

Perhaps the server evaluates datetime.now () when starting the server, try to make it lazy through the template or use a variable in your view.

Take a look at this blog post .

+1
source share

Django sets the system time zone based on your TIME_ZONE settings variable. This can lead to all the confusion when starting multiple instances of Django with different TIME_ZONE settings.

This is what Django does:

 os.environ['TZ'] = self.TIME_ZONE 

The above answer:

"I found that installing wsgi in daemon mode works"

not working for me ...

I think I will no longer use the django built in TIME_ZONE.

+1
source share

you may need to specify the type of content, for example:

 def servertime(): return HttpResponse( datetime.now(), content_type="text/plain" ) 

another idea:

it may not work because datetime.now () returns a datetime object. Try the following:

 def servertime(): return HttpResponse( str(datetime.now()), content_type="text/plain" ) 
0
source share

Try setting your time zone (TIME_ZONE variable) in settings.py

It worked for me.

0
source share

All Articles