当后台中的表单验证失败时,如何在jQuery对话框中重新打开Django表单? [英] How to reopen a Django form in a jQuery dialog when the form validation fails in the backend?

查看:108
本文介绍了当后台中的表单验证失败时,如何在jQuery对话框中重新打开Django表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django表单,我在用户点击了一个网页上的一个链接后,动态加载到一个jQuery对话框。链接中的href指向只包含表单内容的Django页面,而不是整个网站布局。

I have a Django form which I load dynamically to a jQuery dialog after a user has clicked a link on a webpage. The href in the link points to a Django page that contains just the form contents, not the whole site layout.


        $('#add-note').click(function() {
            $('#dialog').load($(this).attr('href')).dialog({
                title: 'Add a note',
                modal: true,
                draggable: false,
                minWidth: 500,
            });

            return false;
        });

如果用户提交在后台中验证的表单,这可以正常工作。但是,如果表单包含验证错误,Django将浏览器转发回表单页面,在这种情况下,实际上并不是用户目前正在查看的页面。

This works fine if the user submits a form that validates in the backend. However, if the form contains validation errors, Django forwards the browser back to the form page, which in this case is not actually the page the user was viewing at the moment.

看到,用户在一个完全不同的页面,表单只是动态加载到jQuery对话框。所以问题是,处理这种情况最好的办法是什么?如何打开带有验证错误的表单回到对话框,而不是表单页面?

See, the user was on a totally different page and the form was just dynamically loaded to the jQuery dialog. So the question is, what would be the best way to handle this kind of situation? How do I open the form with validation errors back in to the dialog and not the form page itself?

所有帮助非常感谢!

推荐答案

当使用AJAX和Django表单时,我通常使用以下方法(没有代码示例,但我认为你会得到想法)。

When using AJAX and Django forms I usually do it the following approach (no code examples though, but I think you will get the idea).


  • 创建一个不同的模板,只是为了呈现此表单(没有块,不从另一个模板延伸)

  • 这使得模板显示窗体,通过 django.template.loader.render_to_string (使用我提到的模板)将此表单直接呈现到html中。将这个渲染的表单放在将其推送到模板的上下文中。

  • 在您的模板中,为表单创建一个容器并在其中呈现html。

  • 当您现在通过AJAX提交表单时,请确保提交表单的处理程序也会呈现表单(如通过 django.template.loader.render_to_string 在原始视图中)并至少返回这个html。如果表单无效,则会包含错误。

  • 获取AJAX响应时,只需简单地替换容器中的表单。之后,你得到了Django表单,包括原始django表单提交的所有错误。

  • create a different template just for rendering this form (no blocks, no extending from another template)
  • in your view which render the template to display the form, render this form direclty into html via django.template.loader.render_to_string (using the template I mentioned). Put this rendered form into the context pushing it to the template.
  • In your template create a container for the form and render the html inside there.
  • when you now submit your form via AJAX make sure the handler which submit the form renders the form as well (like in your original view via django.template.loader.render_to_string) and returns at least this as html. Thls will include the errors if the form was not valid.
  • When getting the AJAX response simply replace the form in your container completely. After that you got the Django form including all the errors from a original django form submit.

这种方法的优点是Django是仍然照顾窗体验证和错误显示,并且您不需要通过Javascript来处理它(可能有点烦人,特别是对于字段错误)。缺点当然你必须为它编写更多的后端代码。 :)

The advantages of this approach are that Django is still taking care of the form validation and error displaying and you don't need to take care of it via Javascript (can be a little annoying especially for field errors). Disadvantage is of course that you have to write more backend code for it. :)

这篇关于当后台中的表单验证失败时,如何在jQuery对话框中重新打开Django表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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