def test_create_render_bool_template_tag_loads_template(self, mock_loader): mock_loader.get_template.return_value = DjangoTemplate( Template("Hello: {{ str }}"), mock_loader) func = flag_utils.create_render_bool_template_tag( { 1: { "str": "person" }, 2: { "str": "animal" } }, "test.html") self.assertEqual(func(1), "Hello: person") self.assertEqual(func(2), "Hello: animal") # check that loader was called with our "custom" template self.assertEqual(mock_loader.get_template.call_count, 1) self.assertEqual(mock_loader.get_template.call_args, [('test.html', ), {}]) # check default template name func = flag_utils.create_render_bool_template_tag({ 1: { "str": "person" }, 2: { "str": "animal" } }) self.assertEqual(mock_loader.get_template.call_count, 2) self.assertEqual(mock_loader.get_template.call_args, [('inboxen/flags/bool.html', ), {}])
def test_create_render_bitfield_template_tag_singleton(self, mock_loader): template = Template("{% for obj in flags %}Hello: {{ obj.str }}\n{% endfor %}") mock_loader.get_template.return_value = DjangoTemplate(template, mock_loader) func = flag_utils.create_render_bitfield_template_tag({"p": {"str": "person", "singleton": True}, "a": {"str": "animal"}}, "test.html") flag_obj = BitHandler(1 | 2, ["a", "p"]) self.assertEqual(func(flag_obj), "Hello: person\n")
def context_processor_view(request): backend = engines.all()[0] template = Template('{{ string }} {{ number }}') return TemplateResponse( request=request, template=DjangoTemplate(template, backend), )
def test_create_render_bitfield_template_tag_loads_template( self, mock_loader): template = Template( "{% for obj in flags %}Hello: {{ obj.str }}\n{% endfor %}") mock_loader.get_template.return_value = DjangoTemplate( template, mock_loader) func = flag_utils.create_render_bitfield_template_tag( { "p": { "str": "person" }, "a": { "str": "animal" } }, "test.html") flag_obj = (("a", True), ("p", True)) self.assertEqual(func(flag_obj), "Hello: animal\nHello: person\n")
# A couple of request objects - one PJAX, one not. rf = RequestFactory() regular_request = rf.get('/') pjax_request = rf.get('/?_pjax=%23secondary', HTTP_X_PJAX=True, HTTP_X_PJAX_CONTAINER="#secondary") file_template = 'test_template.html' # A template to test the pjax_block decorator. test_template = DjangoTemplate( Template( "{% block title %}Block Title{% endblock %}" "Some text outside the main block." "{% with footwear='galoshes' %}" "{% block main %}I'm wearing {{ colour }} {{ footwear }}{% endblock %}" "{% endwith %}" "{% block secondary %}Some secondary content.{% endblock %}" "More text outside the main block."), template_backend) base_template = DjangoTemplate( Template("{% block main %}base block content{% endblock %}\n" "{% block secondary %}secondary block content{% endblock %}"), template_backend) extends_template = DjangoTemplate( Template( "{% extends base_template %}\n" "{% block secondary %}overridden {{ block.super }}{% endblock %}"), template_backend)