how to check DEBUG true/false in django template -

2020-05-24 19:40发布

I would like distinguish a look of some toolbar in layout.html depending if DEBUG = True or not.

I am aware of this answer using django.core.context_processors.debug but it forces me to use RequestContext instead of Request what I not really like, btw how can I use RequestContext for layout.html which extends base.html?

And generally is there some better way to that than mentioned one or the one using custom template tag?

I am currently on Django 1.7

1条回答
做个烂人
2楼-- · 2020-05-24 20:13

In newer versions of Django it is possible just by specifying INTERNAL_IPS in settings.

For example:

INTERNAL_IPS = (
    '127.0.0.1',
    '192.168.1.23',
)

and then in template just:

{% if debug %}

because context processors responsible for that by default, and the answers from How to check the TEMPLATE_DEBUG flag in a django template? are bit deprecated.

查看更多
登录 后发表回答