yerohin
yerohin

Reputation: 13

Custom context processors don't work in Django 1.8.2

When you load a page with global variable (which should be set and accessible through context via main/context_processors.py), the rendering engine ignores global_item, like if it was not set. But local variables work fine, as they usually do.

views.py:

def global_item(request):
    time = datetime.now()
    context = {
        'time': time,
    }
    return render_to_response(
        'main/global.html',
        context
    )

context_processors.py:

def global_item(request):
    global_item = "global item"
    return {
        'global_item': global_item,
    }

global.html:

<p>
  {{ time }}<br>
  {{ global_item }}
</p>

And while rendering it drops global_item: screenshot

Also, I added a line 'main.context_processors.global_item', to 'context_processors' in settinds.py.

Why isn't it working?

Explore repo on GitHub: https://github.com/yerohin/context_processors_test

Upvotes: 1

Views: 782

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

You are not using a RequestContext to render your template. Request processors don't run with a normal context.

Instead of using render_to_response, use the render shortcut, which takes request as the first parameter and uses a RequestContext internally:

return render(request, 'main/global.html', context)

This is nothing to do with the Django version, though.

Upvotes: 1

Related Questions