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

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

问题描述

在 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?

更新:更多解释我为什么要这样做.我有两个 Web 应用程序(我们称它们为 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 是这里最好的原则.我将这两个应用程序合二为一,让事情变得更简单、更强大;我应该能够说服客户这是最好的方法.感谢所有伟大的反馈.如果我要像描述的那样维护两个应用程序,那么我认为 session 将是做到这一点的方式 - 感谢 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 中的 POST 传递到正确的视图(当然,如果数据不敏感).

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:如何重定向帖子并传递帖子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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