问题:Django的render_to_string函数的Flask版本是什么?

因此,我正在尝试通过将这个代码转换为 Flask 来学习 Flask 的 TDD。一段时间以来,我一直在尝试寻找如何将模板呈现为字符串。这是我尝试过的:

render_template(...)
render_template_string(...)
make_response(render_template(...)).data

而且它们似乎都不起作用。

每种情况下的错误似乎是

"...templating.py", line 126, in render_template
    ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

templating.pyrender_template函数中。

我的测试代码如下:

def test_home_page_can_save_POST_request(self):
    with lists.app.test_client() as c:
        c.get('/')
        rv = c.post('/', data = {'item_text':"A new list item"})

        # This test works
        self.assertIn("A new list item", rv.data)

    # This test doesn't
    self.assertEqual(rv.data,flask.make_response(flask.render_template('home.html',new_item_text='A new list item')).data)

home.html如下:

<html>
<body>
    <h1>Your To-Do list</h1>
    <form method="POST">
        <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
    </form>

    <table id="id_list_table">
        <tr><td>{{ new_item_text }}</td></tr>
    </table>
</body>
</html>

编辑:我添加了更多文件,因为错误可能与实际使用的功能无关。我正在使用Celeo在他的回答中建议的内容。

解答

Ceeo 是正确的,但还有两件额外的事情需要考虑(其中一件是 render_template 函数特有的):

首先,看起来您在修改后的函数中存在缩进问题。看起来您在“with”语句之外调用 rv.data。 “assertEqual”语句应该与“assertIn”语句在同一块/缩进级别内。 (看起来你现在已经把它放在了街区之外。)

其次——更重要的是——flask 中的 render_template 函数在输出的 HTML 的开头和结尾添加换行符。 (您可以通过将以下命令打印到标准输出来从 python 交互式 shell 验证这一点:

flask.render_template('home.html',new_item_text='A new list item').data  # adds '\n' at start & end

您将获得的输出将在输出的开头和结尾包含换行符(“\n”)。

因此,您应该尝试使用 strip() 函数剥离输出,如下所示:

def test_home_page_can_save_POST_request(self):
    with lists.app.test_client() as c:
        c.get('/')
        rv = c.post('/', data = {'item_text':"A new list item"})

        self.assertIn("A new list item", rv.data)

        # Suggested Revision
        self.assertEqual(rv.data,flask.make_response(flask.render_template('home.html',new_item_text='A new list item')).data.strip())

这应该可以解决问题。

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐