Skip to content Skip to sidebar Skip to footer

Is It Possible To Check Whether A Context Variable Is Already Set In A View Within A Custom Context Processor Definition?

The issue is that in some views, I am manually getting a context variable (let's say 'G') of interest since I use it to find other information in that particular view (i.e.views A,

Solution 1:

defalways_G(request):
    ifnothasattr(request, 'G'):
        {'G':G.objects.get(user=request.user)}

Solution 2:

I just made middleware that sets the variabel G to request.G since I need it on virtually every request anyway. i.e.:

classGuildMiddleware(object):
    defprocess_request(self, request):
        request.G = figure_out_what_G_is()
        returnNone

Now you can use request.G anywhere in your views (and templates if you're using direct_to_template, RequestContext, etc.).

Post a Comment for "Is It Possible To Check Whether A Context Variable Is Already Set In A View Within A Custom Context Processor Definition?"