Login

Raise Exception on invalid template variable

Author:
guettli
Posted:
March 14, 2008
Language:
Python
Version:
.96
Tags:
template_string_if_invalid
Score:
5 (after 5 ratings)

This recipe raises an exception if there is an invalid variable in the template.

See #6766

Note: I don't need this snippet any more, since I don't use the template language any more. For my projects using only python is better (complex logic, simple layout).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# settings.py
class InvalidVarException(object):
    def __mod__(self, missing):
        try:
            missing_str=unicode(missing)
        except:
            missing_str='Failed to create string representation'
        raise Exception('Unknown template variable %r %s' % (missing, missing_str))
    def __contains__(self, search):
        if search=='%s':
            return True
        return False

TEMPLATE_DEBUG=True
TEMPLATE_STRING_IF_INVALID = InvalidVarException()

More like this

  1. Message exception by marinho 6 years, 7 months ago
  2. Fix for the bad behaviour of GenericForeignKey field by pinkeen 4 years, 4 months ago
  3. function tracing decorator by amitu 6 years, 2 months ago
  4. safe(r) monkeypatching scheme for django testing by showell 5 years, 6 months ago
  5. Capture Stack Trace Decorator by LorenDavie 5 years ago

Comments

Please login first before commenting.