Django render_to_string() ignores {% csrf_token %} : <p>I am trying to perform unit tests in Django. I have the following form in <code>index.html</code>:</p>

<pre><code>&lt;form method=POST&gt;
  {% csrf_token %}
  &lt;input name=itemT&gt;
&lt;/form&gt;
</code></pre>

<p>And I am testing if the view render the template correctly:</p>

<p>views.py</p>

<pre><code>def homePage(request):
    return render(request, 'index.html')
</code></pre>

<p>tests.py:</p>

<pre><code>request = HttpRequest()

response = homePage(request)
if response:
    response = response.content.decode('UTF-8')

expectedHTML = render_to_string('index.html')
self.assertEqual(response, expectedHTML)
</code></pre>

<p>The <code>response</code> has a hidden input field with a csrf token; however, the <code>expectedHTML</code> does not (there is just a blank line at the place of <code>{% csrf_token %}</code>). So the assertion always fails.</p>

<p>Is it possible to have <code>render_to_string()</code> generate a csrf input field? If so, would the token of <code>response</code> the same as that of <code>expectedHTML</code>?</p>

<p>Or, is there any way to ignore the input field such that the test can be successful?</p>
