Django inside RequestContext

Original link: http://www.cnblogs.com/james1207/p/3320127.html

 

    c = RequestContext(request, {
    'foo': 'bar',
    })

    get_template('about.html').render(c)

When we define a RequestContext object when its constructor __init__

Calls get_standard_processors (), returns a collect

 

        collect.extend(_builtin_context_processors)
        collect.extend(settings.TEMPLATE_CONTEXT_PROCESSORS)


This would TEMPLATE_CONTEXT_PROCESSORS and linked settings get set up inside.

 

This process is performed only once, when executed next time, because the

global _standard_context_processors variable non-none.



Further observation of __init__ RequestContext

    def __init__(self, request, dict_=None, processors=None, current_app=None,
            use_l10n=None, use_tz=None):

There is a parameter processors, we can construct objects in time, giving it a temporary processor n


=================Example================================

Info.xml

 

<!DOCTYPE html>
<html>
<head>
	About firstDJ
</head>

<body>
	Thanks for watching me, my address is <b>{{myaddress}}</b>, my request is <b>{{myrequest}}</b>...
</body>
</html>

 


py

 

def custome_proc(request):
    return {'myaddress': request.META['REMOTE_ADDR']}

def hello(request, *args, **kwargs):
    
    c = RequestContext(request, {
    'myrequest': 'milk',
    },
    processors = [custome_proc])

    t = get_template('info.html')
    return HttpResponse(t.render(c))


result

 




 

Reproduced in: https: //www.cnblogs.com/james1207/p/3320127.html

Guess you like

Origin blog.csdn.net/weixin_30487701/article/details/94985707