【问题标题】:how to check DEBUG true/false in django template - exactly in layout.html [duplicate]如何在 django 模板中检查 DEBUG true/false - 完全在 layout.html [重复]
【发布时间】:2014-11-05 04:00:49
【问题描述】:

我想根据 DEBUG = True 与否来区分layout.html 中某些工具栏的外观。

我知道 answer 使用 django.core.context_processors.debug 但它迫使我使用 RequestContext 而不是 Request 我不太喜欢,顺便说一句,我怎样才能将 RequestContext 用于 layout.html 扩展 base.html ?

通常有什么比提到的或the one using custom template tag更好的方法吗?

我目前使用的是 Django 1.7

【问题讨论】:

标签: python django django-templates django-settings


【解决方案1】:

在较新版本的 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? 的答案已被弃用。

【讨论】:

  • JFTR,它也适用于 Django 1.8
  • 它也适用于 Django 1.9
  • 也适用于 Django 1.10
  • 该列表指的是客户端 IP,因此0.0.0.0 没有意义。
  • 2.2.2 确认
猜你喜欢
  • 1970-01-01
  • 2013-03-02
  • 2011-11-16
  • 2016-09-08
  • 2013-11-28
  • 2011-06-25
  • 2010-10-05
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多