django - 从通用应用程序视图返回表单错误

时间:2022-10-24 13:19:03

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

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

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

在模板中,foo表单可以通过get_foo_form模板标记显示:

{% get_foo_form for object as form %}

object is the object which foo will be attached to.

object是foo将附加到的对象。

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.

表单发布到foos应用程序中的视图。如果表格有效,一切都很好。新的foo被保存,视图重定向到前一页(通过隐藏在表单中的'next'参数),新的foo显示得很好。

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.

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

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

有没有办法完成我想要做的事情,或者我应该改变整个结构?

2 个解决方案

#1


All you really need to do is to add the error messages to your dictionary when going back to the page you were at. Then in your HTML, add if tags to check if there are errors and display the appropriate errors if need be.

您真正需要做的就是在返回到您所在的页面时将错误消息添加到您的词典中。然后在HTML中添加if标签以检查是否存在错误,并在需要时显示相应的错误。

You could store your error messages in a list and then have that as one of the dictionary parameters in a call to render_to_response.

您可以将错误消息存储在列表中,然后将其作为render_to_response调用中的字典参数之一。

#2


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.

感谢和+1给AlbertoPL的帮助。

#1


All you really need to do is to add the error messages to your dictionary when going back to the page you were at. Then in your HTML, add if tags to check if there are errors and display the appropriate errors if need be.

您真正需要做的就是在返回到您所在的页面时将错误消息添加到您的词典中。然后在HTML中添加if标签以检查是否存在错误,并在需要时显示相应的错误。

You could store your error messages in a list and then have that as one of the dictionary parameters in a call to render_to_response.

您可以将错误消息存储在列表中,然后将其作为render_to_response调用中的字典参数之一。

#2


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.

感谢和+1给AlbertoPL的帮助。