hi I have an entry model and its modelform .The entry model has a start,end datetimes.I want to prevent the user from mistakenly entering an end datetime value which is before the start datetime.(ie, end=11feb2010 and start=12feb2010 is wrong).I tried to do the error check in an add_entry() view. I gave some print statements to find out values at each stage.
I have given in my entry model definition default values for start,end datetimes as now (ie default=datetime.datetime.now) .Even if I give start time :2009-10-11 09:03:22 end time :2009-10-11 08:03:22(end_time is before start_time - intentionally) the print statements show that form.instance.start_time and form.instance.end_time are taken from the default values where end_time is >start_time This causes datecheck to return True and thus my error check doesn't work. However,the cleaned data shows the values which I entered while creating the entry. I can't understand why the form instance is showing default value.Can anyone tell me? def add_entry(request): print 'in add_entry()' errors={} now=datetime.datetime.now() if request.method=='POST': form=MyEntryForm(request.POST) print 'add_entry():got POST data' print 'form.instance.start_time=',form.instance.start_time print 'form.instance.end_time=',form.instance.end_time if form.is_valid() and date_check(form.instance.start_time,form.instance.end_time): cd=form.cleaned_data print 'add_entry():cleaned data;',cd print 'saving entry' form.save() return redirect('entry_archive_index') else: print 'add_entry():POST:invalid form' errors.update(form.errors) form=MyEntryForm() form=MyEntryForm() print 'add_entry()GET' return render_to_response('myapp/add_entry.html', {'now':now,'entryform':form,'errors':errors}) def date_check(start,end): if start < end: return True else: return False Here is the output of print statements ============ [11/Feb/2010 09:03:45] "GET /myapp/entries/addentry/ HTTP/1.1" 200 2181 in add_entry() add_entry():POST:form got from POST data form.instance.start_time= 2010-02-11 09:04:32.093058 form.instance.end_time= 2010-02-11 09:04:32.093152 add_entry():cleaned data; { 'start_time': datetime.datetime(2009, 10, 11, 9, 3, 22), 'end_time' : datetime.datetime(2009, 10, 11, 8, 3, 22)} saving entry [11/Feb/2010 09:04:32] "POST /myapp/entries/addentry/ HTTP/1.1" 302 0 ======== So I rewrote the error check based on cleaned_data errors={} if form.is_valid(): cd=form.cleaned_data print 'add_entry():cleaned data;',cd start_t=cd['start_time'] end_t=cd['end_time'] if not date_check(start_t,end_t): date_error={'datetime_error':'starttime should be before endtime !'} errors.update(date_error) return render_to_response('pomlogger/pomentry_add_entry.html', {'now':now,'entryform':form,'errors':errors}) print 'saving entry' form.save() return redirect('pomlog_entry_archive_index') else: print 'add_entry():POST:invalid form' errors.update(form.errors) form=PomEntryForm() return render_to_response('pomlogger/pomentry_add_entry.html', {'now':now,'entryform':form,'errors':errors}) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.