当后端表单验证失败时,如何在 jQuery 对话框中重新打开 Django 表单?
我有一个 Django 表单,在用户单击网页上的链接后,我将其动态加载到 jQuery 对话框中。链接中的 href 指向仅包含表单内容的 Django 页面,而不包含整个站点布局。
$('#add-note').click(function() { $('#dialog').load($(this).attr('href')).dialog({ title: 'Add a note', modal: true, draggable: false, minWidth: 500, }); return false; });
如果用户提交在后端验证的表单,则效果很好。但是,如果表单包含验证错误,Django 会将浏览器转发回表单页面,在这种情况下,该页面实际上并不是用户当前正在查看的页面。
看,用户位于完全不同的页面上,表单只是动态加载到 jQuery 对话框中。那么问题来了,处理这种情况的最佳方法是什么?如何打开带有验证错误的表单返回到对话框而不是表单页面本身?
非常感谢所有帮助!
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; });
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.
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?
All help appreciated greatly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用 AJAX 和 Django 表单时,我通常采用以下方法(虽然没有代码示例,但我想您会明白的)。
django.template.loader.render_to_string
在您的原始视图中一样)并至少将其返回为 html 。如果表单无效,这将包括错误。这种方法的优点是 Django 仍然负责表单验证和错误显示,并且您不需要通过 Javascript 来处理它(可能有点烦人,特别是对于字段错误)。缺点当然是你必须为它编写更多的后端代码。 :)
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
(using the template I mentioned). Put this rendered form into the context pushing it to the template.django.template.loader.render_to_string
) and returns at least this as html. Thls will include the errors if the form was not valid.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. :)
如果表单无效,我会返回一个错误和一条消息,而不是返回 200、成功等。在视图中,它是这样的:
在我刚刚发出的 ajax 请求中,请求将失败,我将在某处(通常在对话框中)向用户显示错误,并且 javascript 将如下所示:
If a form isn't valid, I return an error along with a message instead of returning 200, success, etc. In the view it's something like this:
In the ajax request I just made the the request will fail, and I'll display the error to the user somewhere (usually in the dialog) and the javascript will look something like this:
一个快速而肮脏的非ajax解决方案是:
或者如果你有一个表单集:
注意:formset.errors将始终为true(如果没有任何错误,它是一个空字典列表),所以你必须使用is_bound和is_valid 以这种方式,否则您将始终在提交表单集后打开该对话框。
编辑:
这实际上并不能完全回答最初的问题。我发现您的表单是从不同的网址加载的。如果您在同一视图中处理表单并且只是将其显示在同一页面上的 jQuery 对话框中,我的解决方案确实有效。
A quick and dirty, non-ajax solution would be:
Or if you have a formset:
Note: formset.errors will always be true (it's a list of empty dictionaries if there aren't any errors) so you have to use is_bound and is_valid in this way or you'll always open the dialog after submitting the formset.
Edit:
This doesn't actually quite answer the original question. I oversaw that your form was loaded from a different url. My solution does work if you're handling the form in the same view and you're simply displaying it in a jQuery dialog on that same page.