Reputation: 46218
def editor(request):
form = SessionForm(initial={
'end_time': datetime.datetime.now(),
})
if request.method == 'POST':
form = SessionForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('planner/editor.html',
{'form': form}, context_instance=RequestContext(request),)
This view displays the form and re-displays it on error, so there are 2 cases:
In the template, I'm trying to display the field end_time
with a date filter
<div>End value: {{ form.end_time.value }}</div>
<div>End value filtered: {{ form.end_time.value|date:"Y-m-d" }}</div>
Case 1 (initialized)
End value: 2012-04-23 12:30:00
End value filtered: 2012-04-23
Case 2 (on error)
End value: 2012-04-23 12:30:00
End value filtered:
Now let's try to remove the .value
of end_time
<div>End value: {{ form.end_time.value }}</div>
<div>End value filtered: {{ form.end_time|date:"Y-m-d" }}</div>
Case 1 (initialized)
End value: 2012-04-23 12:30:00
End value filtered:
Case 2 (on error)
End value: 2012-04-23 12:30:00
End value filtered: 2012-04-23
As you can see it's doing the inverse.
How can this be explained ?
Upvotes: 1
Views: 2289
Reputation: 46218
Using
{{ form.instance.end_time|date:"Y-m-d" }}
Instead of
{{ form.end_time|date:"Y-m-d" }}
Seems to works in both cases
Upvotes: 3