0

我从 Django 测试中得到一个奇怪的错误,当我测试 Django 或单元测试我的故事应用程序时,我得到了这个错误。它抱怨名称为“内容”的多个块标签,但我已重命名所有标签,因此名称内容的块标签应该为零。该测试甚至从未命中我的应用程序代码,并且在我运行 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
4

2 回答 2

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'网址不存在?

于 2014-10-29T16:04:22.490 回答
0

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

这个错误糟透了。这是我的 404.html 页面,其中包含多个令人讨厌的内容实例。但痕迹从未真正表明这一点。

我终于只是从命令行使用了 grep 。

grep content templates/*

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

感谢maazza 的这个答案的启发。

于 2014-02-26T15:14:09.893 回答