Django:如何重定向一个帖子并传递post数据 [英] Django: How do I redirect a post and pass on the post data

查看:591
本文介绍了Django:如何重定向一个帖子并传递post数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django views.py文件中处理POST请求时,我有时需要将其重定向到另一个URL。我重定向到的url由同一个Django views.py文件中的另一个函数处理。有没有办法这样做,并维护原始的POST数据?

When processing a POST request in the Django views.py file, I sometimes need to redirect it to another url. This url I'm redirecting to is handled by another function in the same Django views.py file. Is there a way of doing this and maintaining the original POST data?

更新:为什么我想做更多的说明。
我有两个网络应用程序(让我们称之为AppA和AppB),它接受用户输入到文本字段中的数据。当用户单击提交时,将处理数据并显示详细的结果。 AppA和AppB期望不同类型的数据。有时用户误将AppB类型的数据发布到AppA。当这种情况发生时,我想将它们重定向到AppB并显示AppB结果,或者至少填充了他们在AppA中输入的数据。

UPDATE: More explanation of why I want to do this. I have two web apps (let's call them AppA and AppB) which accept data entered into a text field by the user. When the the user clicks submit, the data is processed and detailed results are displayed. AppA and AppB expect different types of data. Sometimes a user mistakenly posts AppB type data to AppA. When this happens I want to redirect them to AppB and show the AppB results or at least have it populated with the data they entered into AppA.

另外:


  • 客户端需要两个单独的应用程序,而不是

  • The client wants two separate apps rather than combining them into just one.

我不能显示代码,因为它属于客户端。

I can't show the code as it belongs to a client.

更新2:
我已经决定KISS是这里最好的原则。我将两个应用程序组合成一个使事情更简单和更强大的应用程序;我应该能够说服客户,这是最好的方式。感谢所有好的反馈。如果我要按照所描述的方式维护两个应用程序,那么我认为会话将​​是这样做的方式 - 感谢Matthew J Morrison的建议。感谢Dzida他的评论让我想到了设计和简化。

UPDATE 2: I've decided that KISS is the best principle here. I have combined the two apps into one which makes things simpler and more robust; I should be able to convince the client it's the best way to go too. Thanks for all the great feedback. If I was going to maintain two apps as described then I think sessions would be the way to do this - thanks to Matthew J Morrison for suggesting that. Thanks to Dzida as his comments got me thinking about the design and simplification.

推荐答案

如果你面对这样的问题,您可能需要修改您的设计。

If you faced such problem there's a slight chance that you might need to revise your designs.

这是限制HTTP的POST数据不能与重定向。

你可以描述你想完成什么,也许我们可以考虑一些整洁的解决方案。

Can you describe what are you trying to accomplish and maybe then we can think about some neat solution.

如果你不想使用Matthew建议您可以将GET中的POST参数传递给新页面(考虑一些限制,如查询字符串中的GET参数的安全性和最大长度)。

If you do not want use sessions as Matthew suggested you can pass POST params in GET to the new page (consider some limitations such as security and max length of GET params in query string).

更新到你的更新:)
对我来说听起来很奇怪,你有2个网络应用程序,这些应用程序使用一个views.py(我是对吗?无论如何,请考虑将您的GET中的数据从GET传递到正确的视图(如果数据当然不敏感)。

UPDATE to your update:) It sounds strange to me that you have 2 web apps and those apps use one views.py (am I right?). Anyway consider passing your data from POST in GET to the proper view (in case data is not sensitive of course).

这篇关于Django:如何重定向一个帖子并传递post数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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