pktangyue
pktangyue

Reputation: 8524

How to get dictionary of RequestContext imported from django.template

I have the following code in my django project.

ctxt = RequestContext(request, {
    'power': power,
    'attack': attack,
    'defense': defense,
    })  

Now I want get this dictionary like below through ctxt

{
    'power': power,
    'attack': attack,
    'defense': defense,
}

I tried ctxt.dicts, but this contains too many items. So I see into the source code, and find these code in class RequestContext(Context):

for processor in get_standard_processors() + processors:
    self.update(processor(request))

which I think bring in the other items.

So how can I get that?

Btw, if you want to know why I want to do this, you can see this question I asked before.

How can I get a rewritten render_to_response to get a json in django with the least changes to the whole project

Upvotes: 9

Views: 3562

Answers (2)

kavdev
kavdev

Reputation: 489

I'm a bit late to the party, but you can get a dict from your RequestContext object with

ctxt.flatten()

if you're using Django>=1.7. (Docs)

Upvotes: 19

Lukasz Koziara
Lukasz Koziara

Reputation: 4320

ctxt.dict is stack of dictionaries, so you only need to:

ctxt.dict[0]

Upvotes: 2

Related Questions