input data will be lost if there are errors in the form

9 views
Skip to first unread message

Jeff Hsu

unread,
May 29, 2013, 10:31:05 PM5/29/13
to django...@googlegroups.com
Hi,

    I find this very interesting.  In my html template form:

<form id="student-question" action="{% url 'app:poll' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
 
{{ form.title.errors}}
<div class="span12 question-title-form">
{{form.title}}
                                        .................
This is fine, and it will show the previous inputs if there are errors in the form, however, if I try to customize the html input field, for example, as below, instead of using automatic generated {{form.title}}
                                  <textarea id="titleInput" placeholder="Question Title" name="title" cols="50" rows="1"></textarea> 
                           
The previous inputs will be lost if there are errors in the form.  Does anybody know why?  I need some direction and explanation for this.  Thank you

Jeff



Tom Evans

unread,
May 30, 2013, 6:55:23 AM5/30/13
to django...@googlegroups.com
If you output the fields yourself, you are responsible for ensuring
that the value, name, id - everything, in fact - is correct. In this
case, when you are outputting your textarea manually, you are omitting
the value, either from the initial value passed in, or from the
submitted data when redisplaying a bound form that has errors.

So, you can either do all that work yourself for each field, or you
can allow django to display the field, and manipulate django so that
it creates your HTML as you like it. For instance, you can pass
additional attributes to the widget used to render the field, read
more about widgets in the docs:

https://docs.djangoproject.com/en/1.5/ref/forms/widgets/

Cheers

Tom

Jeff Hsu

unread,
May 30, 2013, 9:49:10 PM5/30/13
to django...@googlegroups.com
Thank you, Tom.  It's exactly what I need.



--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2BMKRHHPj38/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, 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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Reply all
Reply to author
Forward
0 new messages