代碼:如何在提交失敗後保留django中的表單域?從views.py
def feedback(request):
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
form.save()
else:
print("form.errors:", form.errors)
else:
form = CommentForm()
articles = Comment.objects.all()
ResponseDict = {"articles": articles, "form": form}
return render_to_response("feedback.html", ResponseDict,
context_instance = RequestContext(request))
我已經試過這一點,幾次修改的答案類似的問題,但沒有任何工程。當我按提交按鈕時,html中的所有表單字段都將變爲空白。
編輯:代碼feedback.html:
{% extends "base.html" %}
{% block main %}
<table>
<form action="/feedback/" method="POST">
{% csrf_token %}
<div class="article">
<label for="name">
Ваше имя:
</label>
<br />
<input type="text" name="name" id="name" size="40" class="inputbox" value="" />
<br />
<!-- class="inputbox required" -->
<textarea class="WithoutTinymce" cols="50" rows="10" name="text" id="text"></textarea>
<br />
<input type="submit" name="submit" value="Отправить">
</div> <!-- /article -->
</form>
</table>
{% include "articles.html" %}
{% endblock %}
我可以從base.html文件,如果需要也將代碼粘貼。
EDIT2:從base.html文件最小代碼:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
...
<body id="body-id" onload="loaded()">
<!-- Main -->
<div id="main" class="box">
<div id="page" class="box">
<div id="page-in" class="box">
<!-- Content -->
<div id="content">
{% block main %}
{% endblock %}
<hr class="noscreen" />
</div> <!-- /content -->
</div> <!-- /page-in -->
</div> <!-- /page -->
</div> <!-- /Main -->
</body>
</html>
你的代碼看起來有效,你確定你發送帶有'POST'法的形式? –
發佈你的表單模板源代碼請致電 – jpic
@fastreload:是 – DSblizzard