Textarea and initial value

681 views
Skip to first unread message

Marcin Szamotulski

unread,
Sep 29, 2013, 5:58:38 PM9/29/13
to Django Users
Dear Django users,

The short question is: How to use Textarea widget with initial value?
The long version: I have a form with a simple Textarea widget

class Form(forms.Form):

f = forms.CharField(widget=forms.Textarea())

in a view I set an initial value:

form = Form(initial={'f': 'initial value})

and it renders on a page as
<textarea>intial value</textarea>


Then let say I modify the initial value to 'new value'

Then when I POST the form (i.e. send a POST request). The
request.POST.getlist('f')
returns
['new value', 'intial value']
and since the 'initial value' is after the 'new value' when I intialise
the form
form = Form(request.POST)
it has form.cleaned_data['f'] == 'initial value'.

What is the correct way to make initial value in a textarea so that it
is not submitted with the form.

Playing with this I tried to remove the 'initial value' using javascript
($('textarea[name="f"]).html('')), but the submitted date (request.POST)
was not affected.

Thanks for help,
Marcin

Daniel Roseman

unread,
Sep 30, 2013, 9:38:57 AM9/30/13
to django...@googlegroups.com
How are you "modifying the initial value to 'new value' "? If you're simply typing in the field to replace the text that's there, there's no way that request.POST can contain the original data. You seem to be doing something odd somewhere.
--
DR.

Marcin Szamotulski

unread,
Sep 30, 2013, 10:41:47 AM9/30/13
to django...@googlegroups.com
I don't do anything odd: I simply modify it by typing in side textarea.
The page has a very simple JavaScript which does not do anything with
the textarea (though I double-checked that turning it off does not
improve the situation). Inspecting the request reveals that the data is
indeed submitted twice (thus django correctly interpret the situation).

The form is using enctype='multipart/form-data' since it is also used to
submit a file, though leaving only the textarea inside the form and
removing the enctype does not help.

I also checked this in both firefox and chrome and both show that the
POST data for this textarea is submitted twice.

Any ideas?

Thanks for help,
Marcin

Daniel Roseman

unread,
Sep 30, 2013, 11:11:05 AM9/30/13
to django...@googlegroups.com
On Monday, 30 September 2013 15:41:47 UTC+1, Marcin Szamotulski wrote:

I don't do anything odd: I simply modify it by typing in side textarea.
The page has a very simple JavaScript which does not do anything with
the textarea (though I double-checked that turning it off does not
improve the situation).  Inspecting the request reveals that the data is
indeed submitted twice (thus django correctly interpret the situation).

The form is using enctype='multipart/form-data' since it is also used to
submit a file, though leaving only the textarea inside the form and
removing the enctype does not help.

I also checked this in both firefox and chrome and both show that the
POST data for this textarea is submitted twice.

Any ideas?

Thanks for help,
Marcin

Can you perhaps post the template fragment that renders the field, and also the rendered HTML?
--
DR. 

Marcin Szamotulski

unread,
Sep 30, 2013, 1:11:11 PM9/30/13
to Django Users
Sure here it is how the form is render:

 <form action='/account/' method='POST' enctype='multipart/form-data'>
<input type='hidden' name='csrfmiddlewaretoken' value='********************' />
<textarea cols="80" id="id_about" name="about" rows="10">
Submit</textarea>
<div class='errors about_errors'>
</div>
<label for="id_format">Format:</label>
<select id="id_format" name="format">
                <option value="t">Text</option>
                <option value="r" selected="selected">reStructuredText</option>
                <option value="m">MarkDown</option>
        </select>
<div class='errors format_errors'>
</div>
<label for="id_image">Profile image:</label>
<input id="id_image" name="image" type="file" />
<div class='errors image_errors'>
</div>
<input type='submit' name='about' value='Submit'>
    </form>

I use jinja2 template language and it is rendered with
    <form action='{{ url('account') }}' method='POST' enctype='multipart/form-data'>
{% csrf_token %}
{{ render_field(aboutform.about, label=False) }}
{{ render_field(aboutform.format) }}
{{ render_field(aboutform.image) }}
<input type='submit' name='about' value='Submit'>
    </form>

where render_field is a simple macro:

{% macro render_field(field, inline=True, after=True, label=True) -%}
    {% if inline and field.field.widget.__class__.__name__ == 'CheckboxInput' -%}
{{ render_boolfield(field, after) }}
    {%- else -%}
{% if label -%}
{{ field.label_tag() }}
{%- endif %}
{{ field.as_widget() }}
<div class='errors {{field.name}}_errors'>
{{ field.errors.as_ul() }}
</div>
    {%- endif %}
{%- endmacro %}

Thanks,
Marcin



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/045cca12-7ec5-4d29-bc6c-4a601eff5996%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Marcin Szamotulski

unread,
Sep 30, 2013, 1:13:48 PM9/30/13
to Django Users
ps. If I render the form with 
<form action='{{ url('account') }}' method='POST' enctype='multipart/form-data'>
        {{ form.as_ul() }}
</form>
I get the same behaviour.

Marcin

Zoltan Szalai

unread,
Sep 30, 2013, 1:18:00 PM9/30/13
to django...@googlegroups.com
try not to use the same value ('about') for the name attribute of the textarea and your submit button.
not sure it helps the situation though.

Daniel Roseman

unread,
Sep 30, 2013, 2:22:06 PM9/30/13
to django...@googlegroups.com
On Monday, 30 September 2013 18:18:00 UTC+1, Zoltan Szalai wrote:
try not to use the same value ('about') for the name attribute of the textarea and your submit button.
not sure it helps the situation though.


I'd say that is the exact cause of the problem, actually.
--
DR. 

Marcin Szamotulski

unread,
Sep 30, 2013, 6:01:52 PM9/30/13
to Django Users
Thanks, I thought I checked this.

Marcin


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages