Render_to_string和response.content.decode()不匹配 [英] Render_to_string and response.content.decode() not matching

查看:204
本文介绍了Render_to_string和response.content.decode()不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写我的第一个 Django 应用程序:

I'm writing my first Django app by following along with this book:

http://chimera.labs.oreilly.com/books/1234000000754/ch05.html#_passing_python_variables_to_be_rendered_in_the_template

在本书中,有一个测试是验证是否按原样返回html。这是测试:

In the book there is a test that is verifying that the html is being returned as it is supposed to. Here is the test:

def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        expected_html = render_to_string('home.html')
        print(expected_html)
        print(response.content.decode())
        self.assertEqual(response.content.decode(), expected_html)

我的测试失败了 assertEqual 测试,因为我使用 Django模板语言在我的HTML中添加了一个 csrf令牌。这是我的HTML页面的样子:

My test is failing on the assertEqual test because I have added a csrf token in my HTML using the Django Template Language. Here is what my HTML page looks like:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>To-Do lists</title>
</head>
<body>
    <h1>Your To-Do list</h1>
    <form method="POST">
            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
            {% csrf_token %}
    </form>

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

我的断言由于 render_to_string 方法不包括令牌。这是我的两个打印语句包含在我的测试打印出来:

My assert is failing due to the render_to_string method not including the token. Here is what my two print statements included in my test print out:

F<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>To-Do lists</title>
</head>
<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></td></tr>
    </table>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>To-Do lists</title>
</head>
<body>
    <h1>Your To-Do list</h1>
    <form method="POST">
            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
            <input type='hidden' name='csrfmiddlewaretoken' value='VAiGvXZLHCjxWEWdjhgQRBwBSnMVoIWR' />
    </form>

    <table id="id_list_table">
        <tr><td></td></tr>
    </table>
</body>
</html>
F.

他在书中没有这个问题(他正在使用 1.8 ),所以我想知道方法行为是否改变了,或者我将如何写这个测试通过。

He doesn't have this problem in the book (he's using 1.8), so I was wondering if the method behavior has changed, or how I would write this test to pass.

推荐答案

请求参数被添加到 render_to_string 。您可以尝试将测试中的行更改为:

The request argument was added to render_to_string in Django 1.8. You could try changing the line in your test to:

expected_html = render_to_string('home.html', request=request)

只需要在Django 1.9+中进行此更改,测试将在Django 1.8中通过。

It's only required to make this change in Django 1.9+, the test passes without the request in Django 1.8.

这篇关于Render_to_string和response.content.decode()不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆