Python self.assertContains失败;不能';在响应中找不到*word*
我很难完全呈现Python self.assertContains失败;不能';在响应中找不到*word*,python,django,django-views,django-templates,Python,Django,Django Views,Django Templates,我很难完全呈现TopicsPage的模板。假设它呈现sub_heading.html,它扩展了listing.html(两个模板位于同一个模板文件夹中)。测试通过了self.assertTemplateUsed()断言 但是,在以下情况下会提出断言错误: self.assertContains(响应“寻找更多?”) AssertionError:False不正确:在响应中找不到“查找更多内容” 当模板已经在使用时,如何获取要呈现的sub_heading.html内容以通过测试?我特意将GET方法
TopicsPage
的模板。假设它呈现sub_heading.html
,它扩展了listing.html
(两个模板位于同一个模板文件夹中)。测试通过了self.assertTemplateUsed()断言
但是,在以下情况下会提出断言错误:
self.assertContains(响应“寻找更多?”)
AssertionError:False不正确:在响应中找不到“查找更多内容”
当模板已经在使用时,如何获取要呈现的sub_heading.html
内容以通过测试?我特意将GET方法的实现设置为pass
,以显示我是如何子类化视图的
test_views.py
class TestTopicsPage__002(TestCase):
'''Verify that a topic and associated information is displayed
on the main page once the topic is created'''
@classmethod
def setUpTestData(cls):
user = User.objects.create_user("TestUser")
python_tag = Tag.objects.create(name="Python")
js_tag = Tag.objects.create(name="JavaScript")
content = {
'heading': "Test Title",
'text': "Mock text content",
'posted': datetime.datetime.now()
}
cls.topic = Topic(**content)
cls.topic.author = user
cls.topic.save()
cls.topic.tags.add(python_tag, js_tag)
def test_topics_main_page_rendered_topics(self):
response = self.client.get(
reverse("listing")
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "topics/sub_listing.html")
self.assertContains(response, "Looking for more?")
views.py
class AbstractListingPage(TemplateView):
template_name = "topics/sub_listing.html"
extra_content = {
'query_links': ['interesting', 'hot', 'week', 'month']
}
def get_context_data(self):
context = super().get_context_data()
context['topics'] = Topic.objects.all()
context['search_form'] = SearchForm()
context['login_form'] = UserLoginForm
return context
def post(self, request):
context = self.get_context_data()
form = context['login_form'](data=request.POST)
if form.is_valid():
resolver = resolve(request.path)
login(request, form.get_user())
if resolver.namespace:
url = f"{resolver.namespace}:{resolver.url_name}"
else:
url = resolver.url_name
return HttpResponseRedirect(
reverse(url)
)
return self.render_to_response(context)
class TopicsPage(AbstractListingPage):
def get(self, request):
pass
listing.html
{% extends 'index.html' %}
{% load static %}
{% block content %}
{% if not topics %}
<h1 class="topics_placeholder">"Whoops...no topics are being talked about"</h1>
<h2>Join the community...NOW!</h2>
{% else %}
{% for topic in topics %}
<ul class="topic_stats">
<li>{{ topic.answers.count }} answers</li>
<li>{{ topic.likes }} likes</li>
<li>{{ topic.views }} views</li>
</ul>
<div class="topic_wrapper">
<h1><a href="{% url 'topics:topic' id=topic.id %}">{{ topic }}</a></h1>
<ul>
{% for tag in topic.tags.all %}
<li><a href="{% url 'topics:tag_listing' tag=tag %}">{{ tag }}</a></li>
{% endfor %}
</ul>
<p>{{ topic.posted }}</p>
<p><a href="{% url 'users:profile' id=topic.author.id %}">{{ topic.author }}</a></P>
</div>
{% endfor %}
{% endif %}
{% endblock content %}
{% block page_branch %}
{% endblock %}
<div class="question-header">
<button class="flex_item_btn button_widget red" type="button">
<a href="{% url 'topics:ask' %}">Ask Question</a>
</button>
</div>
{% extends 'topics/listing.html' %}
{% load static %}
{% block page_branch %}
<h2>Looking for more? Browse the <a href="{% url 'topics:paginated' %}">complete list of questions</a>
or <a href="{% url 'topics:tag_listing' %}">popular tags</a></h2>
{% endblock %}
{%extends'index.html%}
{%load static%}
{%block content%}
{%如果不是主题%}
“哎呀……没有话题在谈论”
加入社区…现在!
{%else%}
{主题%中的主题为%s}
- {{topic.answers.count}}答案
- {{topic.likes}}likes
- {{topic.views}}}视图
{topic.tags.all%中标记的%s}
{%endfor%}
{{topic.posted}}
{%endfor%}
{%endif%}
{%endblock内容%}
{%block page_branch%}
{%endblock%}
sub_listing.html
{% extends 'index.html' %}
{% load static %}
{% block content %}
{% if not topics %}
<h1 class="topics_placeholder">"Whoops...no topics are being talked about"</h1>
<h2>Join the community...NOW!</h2>
{% else %}
{% for topic in topics %}
<ul class="topic_stats">
<li>{{ topic.answers.count }} answers</li>
<li>{{ topic.likes }} likes</li>
<li>{{ topic.views }} views</li>
</ul>
<div class="topic_wrapper">
<h1><a href="{% url 'topics:topic' id=topic.id %}">{{ topic }}</a></h1>
<ul>
{% for tag in topic.tags.all %}
<li><a href="{% url 'topics:tag_listing' tag=tag %}">{{ tag }}</a></li>
{% endfor %}
</ul>
<p>{{ topic.posted }}</p>
<p><a href="{% url 'users:profile' id=topic.author.id %}">{{ topic.author }}</a></P>
</div>
{% endfor %}
{% endif %}
{% endblock content %}
{% block page_branch %}
{% endblock %}
<div class="question-header">
<button class="flex_item_btn button_widget red" type="button">
<a href="{% url 'topics:ask' %}">Ask Question</a>
</button>
</div>
{% extends 'topics/listing.html' %}
{% load static %}
{% block page_branch %}
<h2>Looking for more? Browse the <a href="{% url 'topics:paginated' %}">complete list of questions</a>
or <a href="{% url 'topics:tag_listing' %}">popular tags</a></h2>
{% endblock %}
{%extends'主题/listing.html%}
{%load static%}
{%block page_branch%}
想要更多吗?浏览
或
{%endblock%}
listing.html底部的内容是孤立的,不存在于父模板中的块中
{% block page_branch %}
{% endblock %}
<div class="question-header">
<button class="flex_item_btn button_widget red" type="button">
<a href="{% url 'topics:ask' %}">Ask Question</a>
</button>
</div>
{%block page\u branch%}
{%endblock%}
由于listing.html扩展了index.html,因此它只能覆盖index.html中存在的块。以上内容必须放在{%block content%}内才能呈现。然后将呈现sub_listing.html对{%block page_branch%}的使用。listing.html扩展index.html。index.html是否有块页面分支?我觉得listing.html的所有内容都应该在block content中,{%block page_branch%}块只在listing.html中。我试图创建两个从listing.html分支出来的模板。假设sub_listing.html和其他模板之间的listing.html内容相同。然而,这两个模板的内容在到达{%block page_branch%}时也是唯一的。我认为它永远不会到达{%block page_branch%},因为它不是最高级别模板中的块,也不包含在最高级别模板中的块中。尝试将{%endblock content%}移动到最底部?这样就完成了任务。