Django:空评论表单错误处理
问题描述:
我使用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')),
然后我送空的形式,我重定向到/评论/后/页。 如何重新加载当前页面并添加错误消息?谢谢!
答
这是正确的行为。见<form action="{% comment_form_target %}" method="post">
?你的表单提交到网址/comments/post/
页面,所以即使它是空白的,你也会在那里结束。你需要做的是在/comments/post/
的视图中添加一些处理方式,这会将你重定向到原始页面,并且会显示一条消息告诉用户填写表单,如果它不完整或不正确。 Django通常会这样做,所以我不知道这里有什么不同。