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

查看:33
本文介绍了在 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.

推荐答案

最好的方法可能是在重定向 URL 上使用编码的查询字符串......这是一种老派的方法.

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天全站免登陆