在Django中使用HttpResponseRedirect时如何传递模板上下文信息? [英] How do I pass template context information when using HttpResponseRedirect in Django?

查看:627
本文介绍了在Django中使用HttpResponseRedirect时如何传递模板上下文信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单在用户输入信息后重定向到同一个页面(以便他们可以继续输入信息)。如果表单提交成功,我将返回

I have a form that redirects to the same page after a user enters information (so that they can continue entering information). If the form submission is successful, I'm returning

HttpResponseRedirect(request.path)

哪个工作正常。但是,我也想在这种情况下向用户显示一些消息(例如,您的数据已保存在屏幕顶部)。如果我没有重定向,我只是在上下文字典中返回这些消息。然而,通过重定向,我不能这样做。

which works fine. However, I'd also like to display some messages to the user in this case (e.g., "Your data has been saved" at the top of the screen). If I weren't redirecting, I'd just return these messages in the context dictionary. With the redirect, however, I can't do this.

那么当使用HttpResponseRedirect时如何传递模板上下文信息?

So how can I pass template context information when using HttpResponseRedirect?

我想要做的似乎是非常常见的,所以请原谅我,如果我缺少一些明显的东西。

What I'm trying to do seems like it would be incredibly common, so please excuse me if I'm missing something obvious.

推荐答案

最好的方法可能是在重定向网址上使用编码的查询字符串...这是一种旧的学校方法。

The best way would probably be to use a coded querystring on the redirect URL... its an old school approach.

你可以做一些类似

/ page /?m = 1,/ page /?m = 2等

/page/?m=1, /page/?m=2, etc

然后,您将在视图代码中使用request.GET解析该变量,并显示相应的消息。

You would then parse that variable with request.GET in the view code and show the appropriate message.

这篇关于在Django中使用HttpResponseRedirect时如何传递模板上下文信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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