Sto scrivendo il mio primo Django
applicazione seguendo insieme a questo libro:Render_to_string e response.content.decode() non corrisponde
Nel libro c'è un test che viene verificato che il html viene restituito come previsto. Ecco il 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)
La mia prova sta venendo a mancare sul test assertEqual
perché ho aggiunto una csrf token
nel mio HTML usando il Django Template Language
. Ecco ciò che la mia pagina HTML appare come:
<!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>
mio assert sta venendo a mancare a causa del metodo render_to_string
escluso il token. Ecco ciò che i miei due print
dichiarazioni incluse nel mio test stampa fuori:
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.
Lui non ha questo problema nel libro (che sta usando 1.8
), quindi mi chiedevo se il comportamento metodo è cambiato, o come Vorrei scrivere questo test per passare.
Il libro Test Driven Development è stato originariamente scritto per Django 1.7. Sembra che questa parte possa essere mancata quando il libro è stato aggiornato a Django 1.8. – Alasdair
autore qui. in realtà è corretto per 1.8, l'errore si verifica quando si passa a 1.9. le istruzioni di installazione nel libro dicono chiaramente che devi attenersi a 1.8, mumble bumble ... – hwjp
@hwjp Errore, il test passa in Django 1.8, le istruzioni di installazione dicono chiaramente di attenersi a 1.8. Stavo usando Django 1.9 quando pensavo di provare 1.8. Oops! – Alasdair