0
votes

I'm getting a strange error from the Django tests, I get this error when I test Django or when I unit test my story app. It's complaining about multiple block tags with the name "content" but I've renamed all the tags so there should be zero block tags with the name content. The test never even hits my app code, and fails when I run django's test suite too.

The application runs fine, but I'm trying to write unit tests, and this is really getting in the way.

Here's the test from story/tests.py:

class StoryViewsTests(TestCase):
    def test_root_url_shows_home_page_content(self):
        response = self.client.get(reverse('index'))
        ...

Here's the view from story/views.py:

class FrontpageView(DetailView):
    template_name = "welcome_content.html"
    def get_object(self):
        return get_object_or_404(Article, slug="front-page")
    def get_context_data(self, **kwargs):
        context = super(FrontpageView, self).get_context_data(**kwargs)
        context['slug'] = "front-page"
        queryset = UserProfile.objects.filter(user_type="Reporter")
        context['reporter_list'] = queryset
        return context

Here's the url from urls.py:

urlpatterns = patterns('',
    url(r'^$', FrontpageView.as_view(), name='index'),
    ...

Here's the template:

{% extends "welcome.html" %}
{% block frontpagecontent %}
        <div>
          {{ object.text|safe}}
            <div class="span12">
              <a href="/accounts/register/">
                <div class="well">
                  <h3>Register for the Nebraska News Service today.</h3>
                </div> <!-- well -->
              </a>
            </div>
          </div>
          <div class="row-fluid">
            <div class="span8">
              <div class="well" align="center">     
                <img src="{{STATIC_URL}}{{ object.docfile }}" />
              </div> <!-- well -->
            </div> <!-- span8 -->
            <div class="span4">
              <div class="well">
            <h3>NNS Staff:</h3>
            {% for r in reporter_list %}
            <p>{{ r.user.get_full_name }}</p>
            {% endfor %}
              </div> <!-- well -->
            </div> <!-- span4 -->
        </div>
{% endblock %}

And here's the trace:

ERROR: test_root_url_shows_home_page_content (story.tests.StoryViewsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/vagrant/webapps/nns/settings/../apps/story/tests.py", line 11, in test_root_url_shows_home_page_content
    response = self.client.get(reverse('about'))
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/test/client.py", line 473, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/test/client.py", line 280, in get
    return self.request(**r)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/test/client.py", line 444, in request
    six.reraise(*exc_info)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 152, in get_response
    response = callback(request, **param_dict)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/views/defaults.py", line 23, in page_not_found
    template = loader.get_template(template_name)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader.py", line 138, in get_template
    template, origin = find_template(template_name)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader.py", line 127, in find_template
    source, display_name = loader(name, dirs)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader.py", line 43, in __call__
    return self.load_template(template_name, template_dirs)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader.py", line 49, in load_template
    template = get_template_from_string(source, origin, template_name)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader.py", line 149, in get_template_from_string
    return Template(source, origin, name)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/base.py", line 125, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/base.py", line 153, in compile_string
    return parser.parse()
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/base.py", line 278, in parse
    compiled_result = compile_func(self, token)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 215, in do_extends
    nodelist = parser.parse()
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/base.py", line 278, in parse
    compiled_result = compile_func(self, token)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 190, in do_block
    nodelist = parser.parse(('endblock',))
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/base.py", line 278, in parse
    compiled_result = compile_func(self, token)
  File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 186, in do_block
    raise TemplateSyntaxError("'%s' tag with name '%s' appears more than once" % (bits[0], block_name))
TemplateSyntaxError: 'block' tag with name 'content' appears more than once
2

2 Answers

0
votes

I almost deleted this, but decided at the last minute to provide the debugging technique. Hopefully it helps someone.

This error sucked. It was my 404.html page that had the offending multiple instances of content. But the trace never actually shows that.

I finally just used grep from the command line.

grep content templates/*

There are a lot of places where content shows up, but limiting the search to only the templates helped make the list to look through manageable. FYI, I have a separate dir for templates outside of the app. You might have to search in a different location, but you could also just cd your way to the template dirs and run grep on each to find offending bits of code. mmmm ... grep.

Thanks to this answer by maazza for inspiration.

0
votes

In re debugging Django stack traces: the trick is spotting that at some point the stack trace crosses over from your code into Django's bailiwick. Unless you've discovered a Django bug, you should be able to limit your search to your own code. (But if you are stumped on what your code is expected to be, looking at either the Django docs or the Django source may still help, of course.)

The stack trace you posted begins like this:

File "/vagrant/webapps/nns/settings/../apps/story/tests.py",
line 11, in test_root_url_shows_home_page_content
  response = self.client.get(reverse('about'))

File "/home/vagrant/django5/local/lib/python2.7/site-packages/django/test/client.py", 
line 473, in get
  response = super(Client, self).get(path, data=data, **extra)

... more Django stuff ...

The second item, already, crosses into Django territory, so probably the error lay in the first line:

response = self.client.get(reverse('about'))

Maybe the 'about' url did not exist?