Form Validation

2 views
Skip to first unread message

rupert

unread,
Jul 30, 2010, 11:42:14 AM7/30/10
to Django users
I'm trying to use this line of code in form validation, and it is not
working:

{{ form.field_name.errors }}


The following lines do output:
{{ form.filed_name}}
{{ form.field_name.help_text }}

Any thoughts? Could it be something in the view?

Here is the index.html page:
#############################################################################
{% extends "feedback/base.html" %}


{% block content %}


<div id="survey-form">
<form action="" method="post" class="survey-form">
<table>
<div id="topForm">
<div class="survey-form">
{{ form.event.errors }}
<label for="id_event">Event you participated in:</label>
{{ form.event }}
</div>
<div class="survey-form">
<label for="id_unit">Your unit:</label>
{{ form.unit }}
{{ form.unit.help_text }}
</div>
<div class="survey-form">
<label for="name">Your name:</label>
{{ form.name }}
{{ form.name.help_text }}
</div>
<div class="survey-form">
<label for="title">Title</label>
{{ form.title }}
{{ form.title.help_text }}
</div>
<div class="survey-form">
<label for="email">Your email address:</label>
{{ form.email }}
{{ form.email.help_text }}
</div>
</div>
<!-- Delete -->
{% if form.name.errors %}
<ol>
{% for error in form.name.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
<!-- Delete Above -->
<!--Middle-->
<div id="middleForm">
<div class="survey-form">
{{ form.errors }}
<p class="question"></p>
<p class="bullets">{{ form.question_1 }}</p>
</div>
<div class="survey-form">
{{ form.question_2.errors }}
<p class="question"></p>
<p class="bullets">{{ form.question_2 }}</p>
</div>
<div class="survey-form">
{{ form.question_3.errors }}
<p class="question"></p>
<p class="bullets">{{ form.question_3 }}</p>
</div>
<div class="survey-form">
{{ form.question_4.errors }}
<p class="question"></p>
<p class="bullets">{{ form.question_4 }}</p>
</div>
<div class="survey-form">
{{ form.question_5.errors }}
<p class="question"></p>
<p class="bullets">{{ form.question_5 }}</p>
</div>
</div>
<!--Bottom -->
<div id="bottomForm">
<div class="survey-form">
{{ form.comment_6.errors }}
<label for="id_comment_6"></label>
{{ form.comment_6 }}
</div>
<div class="survey-form">
{{ form.comment_7.errors }}
<label for="id_comment_7"></label>
{{ form.comment_7 }}
</div>
<div class="survey-form">
{{ form.comment_8.errors }}
<label for="id_comment_8"></label>
{{ form.comment_8 }}
</div>
<div class="survey-form">
{{ form.comment_9.errors }}
<label for="id_comment_9"></label>
{{ form.comment_9 }}
</div>
<div class="survey-form">
{{ form.comment_10.errors }}
<label for="id_comment_10"></label>
{{ form.comment_10 }}
</div>
</div>
<tr>
<th colspan="2">
<div id="submit"><input type="submit" value="Submit" /></div>
</th>
</tr>
</table>
</form>
</div>


{% endblock content %}

Daniel Roseman

unread,
Jul 30, 2010, 11:44:36 AM7/30/10
to Django users
On Jul 30, 4:42 pm, rupert <evan.fer...@gmail.com> wrote:
> I'm trying to use this line of code in form validation, and it is not
> working:
>
> {{ form.field_name.errors }}
>
> The following lines do output:
> {{ form.filed_name}}
> {{ form.field_name.help_text }}
>
> Any thoughts? Could it be something in the view?

Very likely. Has the form been validated?
--
DR.

rupert

unread,
Jul 30, 2010, 11:49:59 AM7/30/10
to Django users
Here's the view template:

def respondant(request):

user = request.user

if set(RESPONDANT_FIELDS).issubset(set([key for key,value in
request.POST.items()])):
form = RespondantForm(request.POST)
if form.is_valid():
form.save()
template = "feedback/success.html"
else:
form = RespondantForm()
return render_to_response('feedback/index.html', {
'form': form,
})

else:
key = request.REQUEST.get('key')
if key:
respondant = Respondant.objects.check_key(key)
else:
form = RespondantForm()
return render_to_response('feedback/index.html', {
'form': form,
})

return render_to_response(template)

Daniel Roseman

unread,
Jul 30, 2010, 11:58:16 AM7/30/10
to Django users
This is an unusual structure for a form view, but the problem happens
in the first `else` clause. Previously you've instantiated the form,
passing it the POSTed values, and checked it for errors. But now you
re-instantiate a *blank* form, which obviously won't have any errors
attached to it. Instead of doing that, you should simply be passing
the existing form object into the template - in other words, you don't
need that `else` clause at all (I'm assuming that the extra
indentation of `return render_to_response` is just a cut-and-paste
error, and it's actually at the same level as the `if form.is_valid()`
line.)
--
DR.
Reply all
Reply to author
Forward
0 new messages