如何处理通过自定义模板标签加载的Django表单的POST请求? [英] How to handle a POST request from a Django form loaded via a custom template tag?

查看:46
本文介绍了如何处理通过自定义模板标签加载的Django表单的POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Bootstrap导航栏中添加了一个Django表单,该表单将包含在每个页面中,并且它将使用适当的值进行渲染.该表单是使用included_tag添加的.但是,我现在对如何处理表单中的请求一无所知.提交后,无论用户在哪个页面上,都应重新加载表单提交中的更新内容.有关更多背景信息,请参阅我之前的问题:

I added a Django form to my Bootstrap nav bar to be included on every page, and it renders as it should with the appropriate values. The form was added using an inclusion_tag. However, I'm now at a loss as to how to handle the request from the form. Upon submission, whichever page the user was on should reload with updated content from the form submission. For more context, see my earlier question: How to place a django form in a nav bar so that it appears on every page?

推荐答案

回答我自己的问题(再次).要处理来自出现在每个页面上并通过自定义模板标签加载的表单的请求,请创建一个url路径和相应的视图-例如"/form-submission/"和form_submission_view.在视图中,按照通常处理POST请求的方式处理表单处理逻辑,但是然后将重定向返回到提交表单时用户所在页面的重定向,例如:

Answering my own question (again). To handle a request from a form that appears on every page and loaded via a custom template tag, create a url path and corresponding view -- e.g. '/form-submission/' and form_submission_view. In the view, handle the form processing logic as you normally would for a POST request, but then return a redirection back to whatever page the user was on when the form was submitted, like so:

return redirect(request.POST.get('path'))

这篇关于如何处理通过自定义模板标签加载的Django表单的POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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