django评论:如何防止表单错误将用户重定向到预览页面?

7 投票
2 回答
1315 浏览
提问于 2025-04-15 13:07

目前,django.contrib.comments在表单出现错误时,会把用户送到预览页面。

我在博客中使用评论功能,如果提交出错,我更希望用户能留在他们原本所在的页面。可是,从我了解的情况来看,这个行为是写死在django.contrib.comments.views.comments.post_comment里的:

# If there are errors or if we requested a preview show the comment
if form.errors or preview:
    template_list = [
        "comments/%s_%s_preview.html" % tuple(str(model._meta).split(".")),
        "comments/%s_preview.html" % model._meta.app_label,
        "comments/preview.html",
    ]
    return render_to_response(
        template_list, {
            "comment" : form.data.get("comment", ""),
            "form" : form,
            "next": next,
        },
        RequestContext(request, {})
    )

有没有什么办法可以在不修改django.contrib.comments源代码的情况下,改变这个行为呢?

任何建议都非常感谢...

谢谢!

2 个回答

0

没错!现在有办法来自定义评论应用了。祝你好运!

3

看起来你有两个实际的选择:

  • 自己写一个视图。你可以先复制那个视图的代码来入手。
  • 对那个视图进行修改,增加一个额外的参数,比如叫'preview_on_errors',默认值是True,但可以被覆盖。然后把这个修改贡献回Django,这样其他人也能受益。

撰写回答