def test_correct_exception_index(self): tests = [ ("{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}", (38, 56)), ( "{% load bad_tag %}{% for i in range %}{% for j in range %}{% badsimpletag %}{% endfor %}{% endfor %}", (58, 76), ), ( "{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% for j in range %}Hello{% endfor %}{% endfor %}", (38, 56), ), ( "{% load bad_tag %}{% for i in range %}{% for j in five %}{% badsimpletag %}{% endfor %}{% endfor %}", (38, 57), ), ("{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}", (18, 37)), ] context = Context({"range": range(5), "five": 5}) for source, expected_error_source_index in tests: template = get_template_from_string(source) try: template.render(context) except (RuntimeError, TypeError) as e: error_source_index = e.django_template_source[1] self.assertEqual(error_source_index, expected_error_source_index)
def load_template(self, template_name, template_dirs=None): key = template_name if template_dirs: # If template directories were specified, use a hash to differentiate key = '-'.join([template_name, hashlib.sha1('|'.join(template_dirs)).hexdigest()]) if key not in self.template_cache: template, origin = self.find_template(template_name, template_dirs) if not hasattr(template, 'render'): try: template = get_template_from_string(template, origin, template_name) except TemplateDoesNotExist: # If compiling the template we found raises TemplateDoesNotExist, # back off to returning the source and display name for the template # we were asked to load. This allows for correct identification (later) # of the actual template that does not exist. return template, origin self.template_cache[key] = template return self.template_cache[key], None
def test_lorem_tag(self): t = loader.get_template_from_string("{% load webdesign %}{% lorem 3 w %}") self.assertEqual(t.render(Context({})), 'lorem ipsum dolor')
def render_template(self, template, **kwargs): if isinstance(template, six.string_types): template = loader.get_template_from_string(template) return template.render(Context(kwargs)).strip()
def test_for(self): source = "{% for i in 1 %}{{ a }}{% endfor %}" template = get_template_from_string(source) vars = template.nodelist.get_nodes_by_type(VariableNode) self.assertEqual(len(vars), 1)
def test_ifchanged(self): source = "{% ifchanged x %}{{ a }}{% endifchanged %}" template = get_template_from_string(source) vars = template.nodelist.get_nodes_by_type(VariableNode) self.assertEqual(len(vars), 1)