Thursday, May 30, 2013

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

On Thu, May 30, 2013 at 3:31 AM, Jeff Hsu <jyhsu1125@gmail.com> wrote:
> 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

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment