如何使用Django重定向功能传递上下文数据? [英] how to pass context data with django redirect function?
问题描述
我具有重定向到URL的功能.
I have function which redirect to a URL.
return redirect('/orders')
URL /orders
具有一些正在传递给它的上下文数据.我想在视图中传递一些附加数据以及URL函数的数据,例如:
The URL /orders
has some context data which is being passed to it.
I want to pass some additional data along with data from the URL's function in view like:
return redirect('/orders', {'message':'some_message'})
我根据文档尝试这样做:
I tried like this according to documentation:
return redirect('/orders', message='some_message')
但是它没有将任何数据传回html.那么如何通过重定向传递数据?
But it didn't passed any data back to html. So how to pass data with redirect?
推荐答案
如果它只是一个小项目,例如?status = 2
,则可以按常规方式将其添加到URL中.(除了一点:小心点,这可能会对搜索引擎搜索您的网站的方式产生负面影响,即造成重复的内容问题)
If its just a small item, like ?status=2
it can be added to the URL in the usual way. (small aside: careful, it may have a negative impact of how search engines spider your web site, i.e. create duplicate content issues)
但是,对于您的示例,将整个消息"字符串传递到下一页,我建议使用相同的方法 Django的会话引擎.
However, for your example, passing an entire "message" string to the next page, I would recommend to do it the same way Django's Messages framework does it: by using Django's Session engine.
这篇关于如何使用Django重定向功能传递上下文数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!