【问题标题】:django comments: how to prevent form errors from redirecting the user to the preview page?django评论:如何防止表单错误将用户重定向到预览页面?
【发布时间】:2009-07-23 20:01:27
【问题描述】:

目前,如果表单有任何错误,django.contrib.cmets 会将用户发送到预览页面。

我在博客上下文中使用 cmets,如果提交出现问题,我更希望用户留在他们所在的页面上。据我所知,这是硬编码在 django.contrib.cmets.views.cmets.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.cmets 的情况下更改此行为?

任何指针将不胜感激......

谢谢!

【问题讨论】:

    标签: python django django-contrib


    【解决方案1】:

    看起来你有两个真正的选择:

    • 编写您自己的视图。可以复制该视图的代码以开始使用。
    • 修补该视图以获取额外参数,例如默认为 True 但可以覆盖的“preview_on_errors”。将补丁回馈给 Django,以便其他人可以从中受益。

    【讨论】:

    • +1 用于编写您自己的包含 post_comment 视图的视图。
    【解决方案2】:

    是的!现在有一种方法可以customize the Comments app。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多