login community 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 Mar 28 '12 at 19:17

spreston42's gravatar image

spreston42
11

edited Mar 28 '12 at 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.

(Mar 28 '12 at 20:39) bmeyer71 ♦♦ bmeyer71's gravatar image

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.

answered Mar 29 '12 at 09:56

seanf's gravatar image

seanf ♦♦
56291220

edited Mar 29 '12 at 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.

(Mar 29 '12 at 16:25) spreston42 spreston42's gravatar image

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

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

Seems to work. We'll see tomorrow.

(Mar 29 '12 at 17:28) spreston42 spreston42's gravatar image

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.

(Mar 29 '12 at 18:32) bmeyer71 ♦♦ bmeyer71's gravatar image

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!

(Apr 08 '12 at 09:36) spreston42 spreston42's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
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

Tags:

×643
×149
×4

Asked: Mar 28 '12 at 19:17

Seen: 681 times

Last updated: Apr 08 '12 at 09:36

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited