gpt4 book ai didi

Django 测试客户端响应包含空模板列表?

转载 作者:行者123 更新时间:2023-12-02 11:38:52 25 4
gpt4 key购买 nike

根据 Django 测试文档,Django 客户端 Response 对象包含"template",即:“用于呈现最终内容的模板实例列表,按照它们呈现的顺序排列。对于列表中的每个模板,如果模板是从文件加载的,则使用 template.name 获取模板的文件名。(该名称是一个字符串,例如“admin/index.html”。)”

但是,尽管我确信模板已渲染,但我得到的是空模板列表。

from django.test.client import Client
c = Client()
response = c.post('/signup/', {'email': 'a@b.com', 'password1': 'smith', 'password2': 'smith'}, follow=True)
print response.templates
# []

为什么模板是空的?我如何查看渲染的模板?

最佳答案

您是否在交互式 session 中尝试过您的代码? Django documentation说:

Although * your code * [+] would work in the Python interactive interpreter, some of the test client's functionality, notably the template-related functionality, is only available while tests are running. The reason for this is that Django's test runner performs a bit of black magic in order to determine which template was loaded by a given view. This black magic (essentially a patching of Django's template system in memory) only happens during test running.

因此,如果您在测试运行中运行它,它应该可以工作。

[+] 我已将 django 文档中的 * 上面的示例 替换为 您的代码*以使此代码段更具可读性。

关于Django 测试客户端响应包含空模板列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7947855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com