当 Django 注释中出现错误时如何重定向回同一页面

发布于 2024-11-26 01:38:15 字数 970 浏览 1 评论 0原文

如果评论提交表单中有错误,如何让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-12-03 01:38:15

你必须使用 HttpResponseRedirect

from django.http import HttpResponseRedirect

def comment_form(request):
    error = request.GET.get('error', None)
    requestDict = {'error': error}
    return render_to_response('comments.html', requestDict, context_instance=RequestContext(request))

def post_comment(request):
    ....
    your code
    ....
    if something_goes_wrong:
        HttpResponseRedirect('project/comment_form/?error=ThereisProblem')

在模板中你可以这样做:

{If error %}
   <h1>{{error}}<h1>
{%else%}
    render comments...
{%endif%}

希望这会帮助你:)

you have to use HttpResponseRedirect

from django.http import HttpResponseRedirect

def comment_form(request):
    error = request.GET.get('error', None)
    requestDict = {'error': error}
    return render_to_response('comments.html', requestDict, context_instance=RequestContext(request))

def post_comment(request):
    ....
    your code
    ....
    if something_goes_wrong:
        HttpResponseRedirect('project/comment_form/?error=ThereisProblem')

And in template you can do this:

{If error %}
   <h1>{{error}}<h1>
{%else%}
    render comments...
{%endif%}

Hope this will help you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文