【发布时间】:2013-12-27 05:44:57
【问题描述】:
我正在制作一个表格,但是当数据无效时,再次加载页面时它会清空该字段,我该如何解决这个问题? 我的看法:
def create_country(request):
if request.POST:
country_form = CountryForm(request.POST)
if country_form.is_valid():
country_form.save()
return HttpResponseRedirect(reverse('country'))
else:
return render(request, 'create_country.html', {'country_form': country_form})
country_form = CountryForm()
return render(request, 'create_country.html', {'country_form': country_form})
还有我的模板:
<div>
<center>
<h2>Create a new country</h2>
</center>
<form action="{% url 'create_country' %}" method="post">{% csrf_token %}
<table>
<tr>
<b>{{ country_form.non_field_errors }}</b>
</tr>
<tr>
<b>{{ country_form.name.errors }}</b>
</tr>
<tr>
<td><label for="id_name">Name:</label></td>
<td><input id="id_name" type="text" name="name" size= "25" maxlength="127" /></td>
</tr>
<tr>
<b>{{ country_form.flavor.errors }}</b>
</tr>
<tr>
<td><label for="id_flavor">Flavor Text:</label></td>
<td><textarea style="width: 300px; height: 200px;" id="id_flavor" type="text" name="flavor" maxlength="511"></textarea></td>
</tr>
<tr>
<td></td>
<td style="float: right;"><input type="submit" value="add country" /></td>
</tr>
</table>
</form>
</div>
如何在帖子返回无效后将旧数据添加回表单中?
【问题讨论】:
-
Jep 好像解决了,谢谢
标签: django forms validation templates