I have this sample of django code:
# views.py
def test_view(request):
form = TestForm(
request.POST or { 'text': 'some text'},
)
data = {
'form': form,
}
print 'before rendering'
return render(request, 'test.html', data)
# forms.py
class TestForm(forms.Form):
text = forms.CharField()
def __init__(self, *args, **kwargs):
print 'init'
super(TestForm, self).__init__(*args, **kwargs)
def clean(self):
print 'in clean'
and this template:
#test.html
<form id='test-form' method="post" action="some url" enctype="multipart/form-data">
{{ form.as_p }}
<input type="submit" value="Save"/>
</form>
when i send get request to this file i have this output in console:
before rendering
init
in clean
when I write {{ form.text }} instead of {{ form.as_p }} I have only:
before rendering
init
It seams to me that as_p method calls clean() internally in process of rendering template.
Before that I mentioned that as_p method only is some kind of shortcut(I understand that it's a method of Form class) and doesn't realize logic.
Why does it happen? Is it a bug or some usefull feature?
Version of Django==1.5.1