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 values for
the datetime fields whereas cleaned_data has the user input values.Can
anyone tell me?
def add_entry(request):
print 'in add_entry()'
errors={}
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',
{'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
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('myapp/add_entry.html',
{'entryform':form,'errors':errors})
print 'saving entry'
form.save()
return redirect('entry_archive_index')
else:
print 'add_entry():invalid form'
errors.update(form.errors)
form=MyEntryForm()
return render_to_response('myapp/add_entry.html',
{'entryform':form,'errors':errors})
This does the correct error checking ..Still I want to know if there
is a better way of doing the error check on start and end dates..The
above code looks like a jumble of if else statements..and I know that
is not the best way of doing things
thanks
harry
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.