In my form I have a field that I want to default as Today when launching the form:
class ExpenseForm(forms.ModelForm):
date = forms.DateField(initial=datetime.now(), widget=forms.DateInput(attrs={'id':'datepicker'}))
Now if I go to the form I see it working correctly, it's got today's date populated, (for example "2018-12-03") but then when I come in tomorrow, it STILL has yesterday's date populated. In fact it will not update until I somehow restart the server.
Any idea?
Thank you.
Related
I'm working on a website that has to do with user entering data to fields of a table. There is (input type=date) in top of the page. I want the user to enter values daily. but I want to save that data in the database according to that day and if the user change the date, I want to clear some fields for that day for a new fresh entry.
I also don't know if there should be a datefield in model or not?? If so, would I have to populate the database with lots of dates? and if it reach the last date, I have to populate it again and so on??
How I go about doing this?
I tried looking for a solution but couldn't find one that seems good. I found I could use crontab in django or scheduler but I don't think this is the optimum solution.
I am working on Python/Django project. I am trying to let user select date and time using jQuery plugin datetimepicker add-on. So when I select now option and post data django is saving the time in UTC offset. This is what is saved in database, 2017-03-30 13:38:00+00:00. I need to convert this time from user's timezone and save it in system as utc. Because later I will have script running which will look for data in database which is less than the utc time.
Actually the script is to let user post information on website and let them chose the publish date and time. So for example, If use posted an article which will be published on April 2nd 1pm Chicago time, I don't want other users to read the article before this time. So people around the world can read article as soon as it is April 2nd and 1PM in Chicago. So how can I make this functionality work?
My solution was to get the time and remove it's timezone information using replace(tzinfo=pytz.timezone('America/Chicago')) and when I print the time, I am getting 2017-03-30 13:38:00-05:51. The actual offset right now is -05:00. Can anyone help me to and tell me what I am doing wrong?
What I am doing for form is that I have publish_date object in my model and I am using django forms to create form. I have added class as an attribute in it and using jquery plugin,
$('.datepicker').datetimepicker({
timeFormat: 'HH:mm',
stepHour: 1,
stepMinute: 1,
});
So when User submits the form, on post method this my code,
form = PublishForm(request.POST)
if form.is_valid():
f = form.save(commit=False)
f.created_by_user_id = request.user.id
f.save()
and than to get the date all I am doing is f.publish_date and the other options I have used lice replace and localize are pretty standard.
Thanks
As noted in the comments, you appear to have two problems. First is that the datetimepicker is saving a date and time with a UTC timezone offset, when it should be applying a different timezone or leaving off the timezone offset entirely. Second is that pytz is using the wrong offset.
I don't know how to fix the first problem, but I can help you with the second. You need to use pytz's localize function. This only works when the datetime doesn't have a timezone attached, but since you know the timezone is incorrect anyway you can delete it first.
tz = pytz.timezone('America/Chicago')
dt = tz.localize(dt.replace(tzinfo=None))
The naming of the datetime replace(tzinfo = ...) function is unfortunate. In fact, its behaviour is random. Do not use this!
Mark's answer is the way to go. Use localize.
I have been facing issue working with leave request. The problem is after I create a leave request and try to fetch the duration of the date_from field in my module field.date 'get_date', I get date as 1 day before . Like if in leave request the 'date_from'=17/11/2015 , it shows on my field as 'get_date'=16/11/2015. The 'date_from' field is saving as 1 day before in database as well. I am not sure is this a timezone issue or I have to use some other method to fetch date to avoid the problem.
I am trying to write a python statement to calculate a user's dob in a mysql db. The dob is a auto-populated field (based off his profile) in a form that a user fills out.
We write python code directly into the fields in our mysql db that perform calculations on runtime. An example:
date.today().strftime("%m-%d-%Y") for today's date.
But I have never used it in reference to another field on the same form, so I am unsure how to do this. I have tried:
(date.today() - dob) / timedelta(days=365.2425).strftime("%m-%d-%Y")
where dob is the field on the form where the user's date of birth is auto-populated.
What am I missing? I receive a 'syntax error', probably because it doesn't know what the dob variable is, but I don't know how to point to that field's value to make it perform the calc. Any help would be appreciated!
So, just in case you find yourself using Python and MySQL, here is the correct answer - this must be placed in a field designated to pull from the db, and depending on the code, it would be:
round(datediff(now(), s.dob))/365.25
or
concat(round(datediff(now(), s.dob))/365.25)
where s.dob is a field in another table. Not sure if this will help anybody, but thought I would post in nevertheless. Thanks!
When using a TimeField in a Django model I wish to be able to perform a query in a view, using the hour and minute elements of the TimeField separately.
However, when I do so, if I run the view I receive an error message that the __hour (or __minute) does not exist (see code below):
scheduleItems = DEVICESCHEDULE.objects.filter(time__hour = nowHour)
If I change the model to use a DateTimeField rather than a TimeField, I am able to use the __hour and __minute options.
I would prefer to keep the field type as a TimeField, as I only need a time to be input and inputting a date (even if it is ignored) is confusing to the user.
Thanks in advance!
Seem like the date/time related lookups only works with dates and datetimes, not the time fields, although seems like it would be a nice feature to have as well.
Have you thought about raising a ticket maybe?
Maybe you can consider this as a replacement:
DEVICESCHEDULE.objects.extra(where=['SUBSTR(time, 1, 2)=%0.2d'], params=[nowHour])
DEVICESCHEDULE.objects.extra(where=['SUBSTR(time, 4, 2)=%0.2d'], params=[nowMinute])