Python Django DetailView - 如何在 get_context_data 中使用“请求”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16243560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:05:51  来源:igfitidea点击:

Django DetailView - how to use 'request' in get_context_data

pythondjango

提问by user2139745

I am trying to modify context data, so I overrided get_context_data. I need the requestvariable to modify this context. So how can I get the requestvariable in get_context-data?

我正在尝试修改上下文数据,所以我覆盖了get_context_data. 我需要request变量来修改这个上下文。那么我怎样才能得到request变量get_context-data呢?

采纳答案by girasquid

You have access to the request in self.request- the third paragraph hereexplains a little more.

您可以访问请求self.request-这里的第三段解释了更多。

EDIT: The text referred to, in case it changes:

编辑:引用的文本,以防它发生变化:

The key part to making this work is that when class-based views are called, various useful things are stored on self; as well as the request (self.request) this includes the positional (self.args) and name-based (self.kwargs) arguments captured according to the URLconf.

完成这项工作的关键部分是,当调用基于类的视图时,各种有用的东西都存储在 self 上;以及请求 (self.request) 这包括根据 URLconf 捕获的位置 (self.args) 和基于名称的 (self.kwargs) 参数。