For some reason, I use my own HTML to display the form. My form input gets the initial value (date) and shows the same form after submitting.
But after sending, the input does not have the previously selected value (only empty).
I use {{ Form.Fileld.value|date:"Ymd" }} in temlate to get the initial and represented values.
It seems that the initial and represented values ββare in different formats: "datetime" for the initial values ββand "string" after sending.
Here is my simple test:
The form
class ShowStartEndDateForm(forms.Form): start_date = forms.DateField(initial=date.today().replace(day=1), label="Start", widget=forms.DateInput(format='%Y-%m-%d'), input_formats=['%Y-%m-%d']) ...
View
if request.method == 'POST': form_date = ShowStartEndDateForm(request.POST) if form_date.is_valid(): form_was_submitted = True cd = form_date.cleaned_data operator = cd['operators'] days=[] for day in range(0,(cd['end_date']-cd['start_date']).days+1): days.append(cd['start_date']+relativedelta(days=+day)) else: form_date = ShowStartEndDateForm() return render_to_response('epay.html', locals(), context_instance=RequestContext(request))
Template
<!- HTML FORM here |classname is my custom template filter. --> Date1: {{ form_date.start_date.value|date:"Ymd" }} \ ({{ form_date.start_date.value|classname }}) Date2: {{ form_date.start_date.value }}
The first call in the browser:
Date1: 2013-10-01 (date) Date2: 1 October 2013
After the submit form:
Date1: (unicode) Date2: 2013-10-01
What am I doing wrong? What is the correct way to access the original and submitted form field values?
You know.
source share