WebFaction
Community site: login faq

It seems the date/clock hangs and doesn't advance unless I restart the apache server. For example, default dates that are supposed to be 'today' show up as yesterday's date until I reset the server.

Also, db queries get the wrong data when I use 'today' as criteria, until I reset the server, then all's well.

Any help is much appreciated.

Info: Django/Python2.7 app on shared hosting here on Webfaction

asked 28 Mar '12, 19:17

spreston42
112
accept rate: 0%

edited 28 Mar '12, 19:20

Can you provide an example field from your model that you are seeing this issue on? Please provide more details if you can.

(28 Mar '12, 20:39) bmeyer71 ♦♦

It sounds to me like you've got a field defined in your model that uses datetime.today() (or some equivalent function) as its default value. The problem with that is that the function gets evaluated only once: at the time the app is started.

Instead of using that function, you should use the auto_now/auto_now_add attributes on your DateField and DateTimeField fields. More information is available in the Django model field reference.

Another option might be to set the default value to the function itself, eg datetime.today, instead of a call to the function, eg datetime.today().

Regarding your "today" queries, it sounds like a similar issue, but it's hard to be sure without seeing your code.

permanent link

answered 29 Mar '12, 09:56

seanf
12.2k42136
accept rate: 37%

edited 29 Mar '12, 10:22

You're exactly right. Here's my code from models.py:

class Run(models.Model): date = models.DateField(default=date.today) distance = models.DecimalField(decimal_places=2, max_digits=5)

However, if I do auto_now, my user can't set another date in the form, correct?

I'm going to have to find a way for the form to do this.

(29 Mar '12, 16:25) spreston42

I took the default out of the Model and put it into the form when called from views.py:

run = JustRanItForm(initial={'date': date.today()})

Seems to work. We'll see tomorrow.

(29 Mar '12, 17:28) spreston42

I think that is going to give you the same problem. As Sean mentioned, you need to call date.today instead of date.today()

Remove the () from the end otherwise it will only be called once at run time.

(29 Mar '12, 18:32) bmeyer71 ♦♦

Removing the () didn't work since I had this in my model, which is only run once (when the server starts). I moved the code into my views.py and sent it to the form. Beginner's mistake.

Thanks for the help!

(08 Apr '12, 09:36) spreston42
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×910
×226
×5

question asked: 28 Mar '12, 19:17

question was seen: 3,007 times

last updated: 08 Apr '12, 09:36

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM