django - 从通用应用程序视图返回表单错误 [英] django - returning form errors from a generic application view

查看:112
本文介绍了django - 从通用应用程序视图返回表单错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发通用应用程序。我们假设它处理可以附加到任何模型的 Foo 对象。



在模板中, Foo 表单可以由 get_foo_form 模板标签显示:

  {%get_foo_form对象作为形式%} 

对象是foo将要的对象



表单发布到 foos 应用程序中的视图。如果表单有效,一切都很好。保存新的 foo ,并将视图重定向到前一个页面(通过表单中隐藏的下一个参数),并且新的 foo 显示得很好。 >

但是如果表单无效,我迷路了。对于同样的情况,类似的应用程序 django.contrib.comments 有一个中间页面,要求用户更正错误。但是,我不想显示中介页面,我想显示前一页的错误。重定向在这里是不够的,因为我需要将错误消息传递到前一页。



有没有办法完成我想要做的,还是应该改变整个结构?

解决方案

如果您有一个应用程序的通用模型,在验证后处理视图中的表单之前将错误消息传递给发布视图的最佳方式似乎是使用会话来存储消息并重定向回来。



感谢并+1,以 AlbertoPL <​​/a>获得帮助。 / p>

I am developing a generic application. Let's assume that it handles Foo objects which can be attached to any model.

In a template, a Foo form can be shown by the get_foo_form template tag:

{% get_foo_form for object as form %}

object is the object which foo will be attached to.

The form posts to a view in the foos application. if the form is valid, everything is fine. the new foo is saved and the view redirects to the former page (via a 'next' argument hidden in the form) and the new foo is displayed nicely.

But if the form is not valid, I'm lost. For the same case, a similar application, the django.contrib.comments has an intermediary page that asks the user to correct the errors. However, I don't want to display an intermediary page, I want to show the former page with the errors. A redirection does not suffice here as I need to pass the error message(s) to the former page.

Is there a way to accomplish what I'm trying to do, or should I change the whole structure?

解决方案

If you have an app featuring a generic model, the best way to pass error messages to the posting view after validating the form in the post handling view seems to be using the session to store the messages and redirecting back.

Thanks and +1 to AlbertoPL for his help.

这篇关于django - 从通用应用程序视图返回表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆