Django 测试客户端响应包含空模板列表?
根据 Django 测试文档,Django 客户端 Response 对象包含“模板”,即:“用于呈现最终内容的模板实例列表,按照它们呈现的顺序排列。对于列表中的每个模板,请使用模板。 name 来获取模板的文件名,如果模板是从文件加载的(名称是一个字符串,例如“admin/index.html”。)”
但是,我得到了一个空的模板列表,即使我是。确信模板已渲染。
from django.test.client import Client
c = Client()
response = c.post('/signup/', {'email': '[email protected]', 'password1': 'smith', 'password2': 'smith'}, follow=True)
print response.templates
# []
为什么模板是空的?如何查看渲染的模板?
According to the Django testing docs, the Django client Response object contains 'templates', which is: "A list of Template instances used to render the final content, in the order they were rendered. For each template in the list, use template.name to get the template's file name, if the template was loaded from a file. (The name is a string such as 'admin/index.html'.)"
However, I am getting an empty list of templates, even though I am confident that a template was rendered.
from django.test.client import Client
c = Client()
response = c.post('/signup/', {'email': '[email protected]', 'password1': 'smith', 'password2': 'smith'}, follow=True)
print response.templates
# []
Why is templates empty? How do I see what template was rendered?
您是否在交互式会话中尝试过您的代码? Django 文档 说:
因此,如果您在测试运行中运行它,它应该可以工作。
[+] 我已将 django 文档中的 * 上面的示例 替换为 您的代码 * 使此代码片段更具可读性。
Have you tried your code in a interactive session? The Django documentation says:
So if you run it in a test run, it should work.
[+] I have replaced the * the above example from the django documentation with your code * to make this snippet more readable.