我有一个重定向用户的视图,并使用Django 消息框架将它们发送到正确的页面,并添加一条带有如下代码的消息:
def new_comment(request,pid):
post = get_object_or_404(DiscussionPost,pk=pid)
if post.closed:
messages.error(request, _('This post is closed. Your comment was not added.'))
return HttpResponseRedirect(reverse("discussionsPost",args=[post.pk]))
现在这对用户来说效果很好,但是在测试消息时不可用。
在单元测试中我做:
response = self.client.post(reverse('aristotle:discussionsPostNewComment',args=[p1.id]),
{'body':"Post is closed, so I can NOT comment."}
)
#the below assertion passes
self.assertRedirects(response,reverse('aristotle:discussionsPost',args=[p1.id]))
print response
print response.context['messages']
第一张印刷品给出:
Vary: Accept-Language, Cookie
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8
Location: http://testserver/discussions/post/1
Content-Language: en
第二次失败并出现错误:
Traceback (most recent call last):
File "/home/ubuntu/workspace/aristotle_mdr/tests/main/test_discussions.py", line 393, in test_post_to_closed_discussion
print response.context['messages']
TypeError: 'NoneType' object has no attribute '__getitem__'
另外,由于没有可以使用messages.get_messages
的请求项目,因此无法使用。
由于 中没有上下文字典HTTPResponseRedirect
,我如何检查消息是否正确发送?