I'm trying to add a context processor custom_proc to pass variables to the template context. Code:
def custom_proc(request): """A context processor that provides 'app', 'user' and 'ip_address'.""" return { 'app': 'MyApp', 'user': request.user, 'ip_address': request.META['REMOTE_ADDR'] } def index(request): template = loader.get_template('core/index-main.html') context = RequestContext( request, { 'message': 'My message' }, processors=[custom_proc] ) return HttpResponse(template.render(context)) At the same time, variables from custom_proc are not included in the template.
If instead of loading the template, I will point it directly to the line:
template = Template('{{ app }} {{ user }} {{ ip_address }}') then the context processor executes and the variables fall into the template.
Searching by code showed that in order to bind variables you need to pass a query to the second parameter in render, but when you try to do:
return HttpResponse(template.render(context, request)) The application crashes with the error:
Internal Server Error: / Traceback (most recent call last): File "E:\var\www\RQ30\venv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner response = get_response(request) File "E:\var\www\RQ30\venv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "E:\var\www\RQ30\venv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\var\www\RQ30\aisog\aisog\core\views.py", line 27, in index return HttpResponse(template.render(context, request=request)) File "E:\var\www\RQ30\venv\lib\site-packages\django\template\backends\django.py", line 64, in render context = make_context(context, request, autoescape=self.backend.engine.autoescape) File "E:\var\www\RQ30\venv\lib\site-packages\django\template\context.py", line 267, in make_context context.push(original_context) File "E:\var\www\RQ30\venv\lib\site-packages\django\template\context.py", line 59, in push return ContextDict(self, *dicts, **kwargs) File "E:\var\www\RQ30\venv\lib\site-packages\django\template\context.py", line 18, in __init__ super(ContextDict, self).__init__(*args, **kwargs) TypeError: dict expected at most 1 arguments, got 3 Question: what am I doing wrong?