﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17529	get_template_from_string default arguments break assertTemplateUsed	Ian Clelland	Unai Zalakain	"`django.template.loader.get_template_from_string` only requires a single argument -- the template source string. The origin and name parameters are optional, and default to None. (This function is never called in the Django codebase with missing parameters, except in the test suite)

If the name parameter is left out, it is explicitly passed as None to the Template constructor, and so the new template receives a name of None, rather than the default ""<Unknown Template>"".

`django.test.testcases.TransactionTestCase.assertTemplateUsed` contains an assertion which joins the 'name' parameter of all of the templates in the request. If any templates used were constructed without a name, the None in the list will cause a !TypeError before the assertion can even be tested.

This is the simplest project I can create which exemplifies this problem:

templatetest/urls.py:
{{{#!python
from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^test/$', 'templatetest.views.test_view', name='test'),
)
}}}

templatetest/views.py:
{{{#!python
from django.template import loader, Context
from django.http import HttpResponse

def test_view(request):
    template = loader.get_template_from_string(""This is a string-based template"")
    return HttpResponse(template.render(Context({})))
}}}

templatetest/tests.py:
{{{#!python
from django.test import TestCase

class TemplateFromStringTest(TestCase):
    def test_none_template_used(self):
        response = self.client.get('/test/')
        self.assertTemplateUsed(response, ""base.html"")
}}}

./manage.py test templatetest:
{{{
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_template_used (templatetest.tests.TemplateFromStringTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/Users/ian/Code/Tests/django-template-test/templatetest/templatetest/tests.py"", line 6, in test_template_used
    self.assertTemplateUsed(response, ""base.html"")
  File ""/Users/ian/.virtualenvs/django-trunk/lib/python2.7/site-packages/django/test/testcases.py"", line 629, in assertTemplateUsed
    (template_name, u', '.join(template_names)))
TypeError: sequence item 0: expected string or Unicode, NoneType found

----------------------------------------------------------------------
Ran 1 test in 0.133s

FAILED (errors=1)
Destroying test database for alias 'default'...
}}}

I believe that either `assertTemplateUsed` needs to guard against None values appearing in the list of template names, or get_template_from_string needs to be modified to call`Template.__init__` in a way which uses *that* class's default parameters, or possibly both. I'll work up a quick patch once I get a feel for the right place to fix this."	Bug	closed	Template system	dev	Normal	fixed	assertTemplateUsed get_template_from_string template testing	claude@…	Accepted	1	0	0	0	0	0
