Django Test Client:测试上下文是否为RequestContext对象

问题描述

| 有没有一种方法可以测试测试客户端返回的response.context对象,RequestContext对象是?我检查了源代码,似乎它们将上下文包装在ContextList对象中。     

解决方法

检查ContextList是否不为空,然后检查作为
RequestContext
对象的项目:
from django.template import RequestContext

response = client.post(...)
context_list = response.context

for context in context_list:
    if isinstance(context,RequestContext):
        # do some more test checks here
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...