[Django] #20703: Why django form.as_p call form.clean method when template renders?

18 views
Skip to first unread message

Django

unread,
Jul 4, 2013, 10:27:49 AM7/4/13
to django-...@googlegroups.com
#20703: Why django form.as_p call form.clean method when template renders?
-----------------------------------+----------------------------------
Reporter: max.kharandziuk@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 1.5
Severity: Normal | Keywords: as_p clean templates
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+----------------------------------
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.
Is it a bug or some undocumented feature?
Should this side effect avoided?

Version of Django==1.5.1

--
Ticket URL: <https://code.djangoproject.com/ticket/20703>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 4, 2013, 10:52:41 AM7/4/13
to django-...@googlegroups.com
#20703: Why django form.as_p call form.clean method when template renders?
-------------------------------------+-------------------------------------
Reporter: max.kharandziuk@… | Owner: nobody
Type: Uncategorized | Status: closed

Component: Template system | Version: 1.5
Severity: Normal | Resolution: invalid
Keywords: as_p clean | Triage Stage:
templates | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* status: new => closed
* needs_docs: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

I think you want to use `initial={'text': 'some text'}` when initializing
`TestForm` rather than `request.POST or {'text': 'some text'}`, but this
ticket tracker isn't the place to get help with this, thanks!

https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels

--
Ticket URL: <https://code.djangoproject.com/ticket/20703#comment:1>

Django

unread,
Jul 4, 2013, 10:57:37 AM7/4/13
to django-...@googlegroups.com
#20703: Why django form.as_p call form.clean method when template renders?
-------------------------------------+-------------------------------------
Reporter: max.kharandziuk@… | Owner: nobody
Type: Uncategorized | Status: closed

Component: Template system | Version: 1.5
Severity: Normal | Resolution: invalid
Keywords: as_p clean | Triage Stage:
templates | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by max.kharandziuk@…):

I don`t ask a help with this code I just mentioned that as_p method has
some side effect(calls clean()) that not described in documentation. Maybe
this effect is a bug

--
Ticket URL: <https://code.djangoproject.com/ticket/20703#comment:2>

Django

unread,
Jul 4, 2013, 11:13:27 AM7/4/13
to django-...@googlegroups.com
#20703: Why django form.as_p call form.clean method when template renders?
-------------------------------------+-------------------------------------
Reporter: max.kharandziuk@… | Owner: nobody
Type: Uncategorized | Status: closed

Component: Template system | Version: 1.5
Severity: Normal | Resolution: invalid
Keywords: as_p clean | Triage Stage:
templates | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timo):

`as_p` will output error rows (if any) as well as non-field errors.
Naturally, this requires calling `clean`. Since you are passing data to
the form on `GET`, the form is validated and `clean` is called.

--
Ticket URL: <https://code.djangoproject.com/ticket/20703#comment:3>

Reply all
Reply to author
Forward
0 new messages