2013-03-01 50 views
0

我使用standart django評論框架。Django:空評論表單錯誤處理

模板:

<h2>Add comment</h2> {% get_comment_form for post as form %} 
<form action="{% comment_form_target %}" method="post" > {% csrf_token %} 
{{form.content_type}}{{form.object_pk}}{{form.timestamp}}{{form.security_hash}} 
Comment:<br /> 
{{form.comment}} 

<input type="hidden" name="next" value="{{ request.get_full_path }}#comment" /> 

<button class="btn btn-large btn-primary" type="submit">Post</button> 

</form> 

網址:

(r'^comments/', include('django.contrib.comments.urls')), 

然後我送空的形式,我重定向到/評論/後/頁。 如何重新加載當前頁面並添加錯誤消息?謝謝!

回答

0

這是正確的行爲。見<form action="{% comment_form_target %}" method="post">?你的表單提交到網址/comments/post/頁面,所以即使它是空白的,你也會在那裏結束。你需要做的是在/comments/post/的視圖中添加一些處理方式,這會將你重定向到原始頁面,並且會顯示一條消息告訴用戶填寫表單,如果它不完整或不正確。 Django通常會這樣做,所以我不知道這裏有什麼不同。