当 Django 注释中出现错误时如何重定向回同一页面
如果评论提交表单中有错误,如何让 Django 评论重定向回您填写评论的同一页面?
所以基本上我有一个这样的模板:
{% block content %}
{% render_comment_form for show %}
{% get_comment_count for show as comment_count %}
<div id="comments-count">
{% if comment_count == 0 %}
No comments yet. Be the first!
{% else %}
Number Of Comments: {{ comment_count }}
{% endif %}
</div>
{% if comment_count > 0 %}
{% render_comment_list for show %}
{% endif %}
{% endblock %}
我创建了自己的 list.html 和 form.html ,一切看起来都很好。在 form.html 模板中有一些这样的代码:
<ul class="form-errors">
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error|escape }}</li>
{% endfor %}
{% endfor %}
</ul>
显然,如果评论提交表单中存在错误,我希望用户看到与以前相同的页面,只是评论表单中显示了一些错误。或者,如果这是不可能的,只需忽略该错误,而不是转换到 Preview.html 模板,它不会保存评论并再次返回到页面。
有什么帮助吗?请注意,理想情况下我不想创建自定义评论应用程序。这个功能应该已经有了。我知道您可以传递下一个变量(我正在这样做),但只有在评论表单成功时它才有效。
How do you get Django comments to redirect back to the same page where you're filling out a comment if there are errors in the comment submission form?
So basically I have a template like this:
{% block content %}
{% render_comment_form for show %}
{% get_comment_count for show as comment_count %}
<div id="comments-count">
{% if comment_count == 0 %}
No comments yet. Be the first!
{% else %}
Number Of Comments: {{ comment_count }}
{% endif %}
</div>
{% if comment_count > 0 %}
{% render_comment_list for show %}
{% endif %}
{% endblock %}
I created my own list.html and form.html and everything looks fine. In the form.html template there is some code like this:
<ul class="form-errors">
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error|escape }}</li>
{% endfor %}
{% endfor %}
</ul>
So obviously, if there is an error in the comment submission form, I would like the user to see the same page as before only with some errors displayed in the comments form. Or alternatively if this is not possible, just ignore the error and instead of transitioning to the preview.html template, it would just not save the comment and again go back to the page.
Any help? Note ideally I dont want to have to create a custom comments app. This functionality should be there already. I know there's a next variable you can pass (and I am doing this), but it only works if the comment form is successful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须使用 HttpResponseRedirect
在模板中你可以这样做:
希望这会帮助你:)
you have to use HttpResponseRedirect
And in template you can do this:
Hope this will help you :)