Show undefined variable errors in Django templates?

后端 未结 8 904
长情又很酷
长情又很酷 2020-12-24 08:50

How can I ask Django to tell me when it encounters, for example, an undefined variable error while it\'s rendering templates?

I\'ve tried the obvious DEBUG = T

8条回答
  •  滥情空心
    2020-12-24 09:21

    Put this in your debug settings:

    class InvalidString(str):
        def __mod__(self, other):
            from django.template.base import TemplateSyntaxError
            raise TemplateSyntaxError(
                "Undefined variable or unknown value for: \"%s\"" % other)
    
    TEMPLATE_STRING_IF_INVALID = InvalidString("%s")
    

    This should raise an error when the template engine sees or finds an undefined value.

提交回复
热议问题