我从 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