Django DetailView——如何在get_context_data中使用“请求”。

时间:2022-11-13 19:22:58

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

我正在尝试修改上下文数据,所以我对get_context_data进行了重载。我需要请求变量来修改这个上下文。那么如何在get_context-data中获取请求变量呢?

1 个解决方案

#1


60  

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

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

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)参数。

#1


60  

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

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

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)参数。