代码之家  ›  专栏  ›  技术社区  ›  andilabs

如何在django模板中检查DEBUG true/false-完全在layout.html中[重复]

  •  45
  • andilabs  · 技术社区  · 10 年前

    我想在 layout.html 这取决于DEBUG是否为True。

    我知道这一点 answer 使用 django.core.context_processors.debug 但它迫使我使用 RequestContext 而不是 Request 我不喜欢的,顺便问一下,我如何使用RequestContext 布局.html 它延伸 base.html ?

    一般来说,是否有比上述方法更好的方法 the one using custom template tag ?

    我目前使用Django 1.7

    1 回复  |  直到 7 年前
        1
  •  78
  •   Udi    6 年前

    在Django的较新版本中,只需指定 INTERNAL_IPS 在设置中。

    例如:

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

    然后在模板中:

    {% if debug %}
    

    因为默认情况下,上下文处理器对此负责 How to check the TEMPLATE_DEBUG flag in a django template? 有点不赞成。