Skip to content Skip to sidebar Skip to footer

Render_to_string And Response.content.decode() Not Matching

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_te

Solution 1:

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)

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

Solution 2:

I found this solution which has worked for the latest Django version - 3.0.6

#add a function to the post request test functiondefremove_csrf_tag(text):
    """Remove csrf tag from TEXT"""return re.sub(r'<[^>]*csrfmiddlewaretoken[^>]*>', '', text)

...
# then change assertion deftest_home_page_can_save_a_POST_request(self):
...
 self.assertEqual(
        remove_csrf_tag(response.content),
        remove_csrf_tag(expected_html)
    )

Post a Comment for "Render_to_string And Response.content.decode() Not Matching"