Django - How to make a variable available to all templates?

后端 未结 2 1472
余生分开走
余生分开走 2020-11-27 12:22

I would like to know how to pass a variable to all my templates, without repeating the same code on every method in my views.py file?

In the example below I would l

2条回答
  •  一整个雨季
    2020-11-27 13:16

    As seen in this example, post Django 1.3 you can simply use render instead of render_to_response which doesn't require you to explicitly pass the context processor.

    def another_view_method(request):
        categories = Category.objects.all()
        return render(
            'eg/front_page.html',
            {'is_logged_in': is_logged_in(request), 'categories':categories}
        )
    

提交回复
热议问题