django 1.6单元测试错误TemplateSyntaxError:阻止标记名称'内容'出现不止一次

时间:2014-02-25 21:06:20

标签: django unit-testing python-2.7 django-templates django-tests

我从Django测试中得到一个奇怪的错误,当我测试Django或我单元测试我的故事应用程序时,我收到此错误。它抱怨名为“content”的多个块标签,但我重命名了所有标签,因此应该有名称内容为零的块标签。测试从来没有打到我的应用程序代码,并且当我运行django的测试套件时也失败了。

应用程序运行正常,但我正在尝试编写单元测试,这实际上是在阻碍。

这是来自story / tests.py的测试:

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

这是来自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

这是来自urls.py的网址:

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

这是模板:

{% 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 %}

这是追踪:

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 个答案:

答案 0 :(得分:0)

我几乎删除了这个,但在最后一刻决定提供调试技术。希望它可以帮助某人。

这个错误很糟糕。这是我的404.html页面,其中包含多个内容实例。但追踪从未真正表明过。

我终于在命令行中使用了grep。

grep content templates/*

有很多内容会显示内容,但将搜索范围仅限于模板有助于使列表易于管理。仅供参考,我在应用程序之外有一个单独的模板目录。您可能必须在不同的位置进行搜索,但您也可以通过cd到模板目录并在每个位置运行grep以查找有问题的代码位。 mmmm ...... grep。

感谢this answer by maazza获取灵感。

答案 1 :(得分:0)

在重新调试Django堆栈跟踪:诀窍是发现在某些时候堆栈跟踪从你的代码跨越到Django的bailiwick。除非您发现了Django错误,否则您应该能够将搜索限制为自己的代码。 (但是如果你对你的代码的预期感到困惑,那么查看Django文档或Django源代码当然可能会有所帮助。)

您发布的堆栈跟踪开头如下:

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 ...

第二项已经跨越Django领域,所以错误可能在第一行:

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

也许'about'网址不存在?