使用上下文处理器访问模板中的 settings.DEBUG

in myapp/context_processors.py:

from django.conf import settings

def debug(request):
  return {'DEBUG': settings.DEBUG}

in settings.py:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'myapp.context_processors.debug',
            ],
        },
    },
]

或者,对于版本<1.9:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'myapp.context_processors.debug',
)

然后在我的模板中,简单地说:

 {% if DEBUG %} .header { background:#f00; } {% endif %}
 {{ DEBUG }}