我有一个基本表单,我使用标准的“{{form.name_of_field.errors}}标签在模板中呈现。当我尝试提交一个空表单时,我希望看到表单重新呈现为”•字段是必需的“在空表单字段旁边。我得到的是一个值错误。”视图wedbpjr.wedb.views.display_prdInboxEntry没有返回一个HttpResponse对象。“我哪里出错?
<form action="." method="POST">{% csrf_token %}
{{ form.non_field_errors }}
<table id="inbox_table">
<tr>
<td colspan="6">
<div class="fieldWrapper">
{{ form.assigned_by.errors }}
<label for="id_assigned_by">Assigned by:</label>
{{ form.assigned_by }}
</div>
</td>
</tr>
<tr>
<td>
<div class="fieldWrapper">
{{ form.job_number.errors }}
<label for="id_job_number">job no:</label><br />
{{ form.job_number }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.cell_number.errors }}
<label for="id_cell_number">cell:</label><br />
{{ form.cell_number }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.job_name.errors }}
<label for="id_job_name">job name:</label><br />
{{ form.job_name }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.request.errors }}
<label for="id_request">request:</label><br />
{{ form.request }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.note.errors }}
<label for="id_note">note:</label><br />
{{ form.note }}
</div>
</td>
<td>
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
{{ form.date_due.errors }}
<label for="id_date_due">date_due:</label>
{{ form.date_due}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</td>
</tr>
<tr>
<td>
<div class="fieldWrapper">
{{ form.box.errors }}
<label for="id_box">inbox:</label><br />
{{ form.box }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.assigned_to.errors }}
<label for="assigned_to">assigned_to:</label><br />
{{ form.assigned_to }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.basecamp_link.errors }}
<label for="id_basecamp_link">basecamp:</label><br />
{{ form.basecamp_link }}
</div>
</td>
<td>
<div class="accepted_by">
{{ form.accepted_by.errors }}
<label for="id_accepted_by">accepted_by:</label><br />
{{ form.accepted_by }}
</div>
</td>
<td>
<div class="fieldWrapper">
{{ form.status.errors }}
<label for="id_status">status:</label><br />
{{ form.status }}
</div>
</td>
<td>
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker2'>
{{ form.completed_on.errors }}
<label for="id_completed_on">completed:</label>
{{ form.completed_on}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker2').datetimepicker();
});
</script>
</div>
</td>
</tr>
<tr>
<td>
<p><input id="inbox_submit_btn" type="submit" value="add" /></p>
</td>
<td>
<p><input id="inbox_delete_btn" name="delete" type="submit" value="delete" /></p>
</td>
</tr>
</table>
</form>
运行Django 1.5.1 - Python 2.7
views.py
def display_prdInboxEntry(request, id):
if request.method == 'POST':
form = PrdInboxForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/production-display/'+ id +'/')
else:
form = PrdInboxForm()
user = request.user
u = UserProfile.objects.get(pk=id)
creativerecords = InboxEntry.objects.filter(box="Creative")
studiorecords = InboxEntry.objects.filter(box="Studio")
records = InboxEntry.objects.filter(assigned_to=u)
return render_to_response('home_inbox.html', {'form': form, 'records': records, 'studiorecords': studiorecords, 'creativerecords': creativerecords, 'user': user}, context_instance=RequestContext(request))
答案 0 :(得分:0)
错误来自您的观点,请确保您的form.is_valid
等于False
。这通常涉及使用不正确的数据重新显示表单
from django.shortcuts import render
from django.http import HttpResponseRedirect
def contact(request):
if request.method == 'POST': # If the form has been submitted...
# ContactForm was defined in the the previous section
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render(request, 'contact.html', {
'form': form,
})
django documentation,清楚地解释了这个