# {% csrf\_token %} not rendering only for first instance in a template

**URL:** <https://forum.djangoproject.com/t/csrf-token-not-rendering-only-for-first-instance-in-a-template/38582>\
**Category:** Templates & Frontend\
**Created:** [February 5, 2025, 2:49pm UTC](https://forum.djangoproject.com/t/csrf-token-not-rendering-only-for-first-instance-in-a-template/38582 "2025-02-05T14:49:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsajip](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vsajip/32/14143_2.png) [@vsajip](https://forum.djangoproject.com/u/vsajip)\
**Post date:** [February 5, 2025, 2:49pm UTC](https://forum.djangoproject.com/t/csrf-token-not-rendering-only-for-first-instance-in-a-template/38582/1 "2025-02-05T14:49:08Z")

</div>

I have a template which contains four forms (in hidden modals, not that that matters). Each form has a `{% csrf_token %}` tag in it. When I render the template in a test, for only the first `{% csrf_token %}` encountered, an empty string is returned; for the other three instances, the `<input type="hidden" name="csrfmiddlewaretoken" ...>` element is rendered correctly.

I can’t quite see what could be happening - it’s a single render from a single GET request. Snippet of test code:

```auto
    def test_home_page(self):
        c = Client(headers=HEADERS)
        resp = c.get('/')
        s = resp.content.decode(resp.charset)
        soup = BeautifulSoup(s, 'html.parser')
        forms = soup.find_all('form')
        for f in forms:
            inp = f.find('input', {'name': 'csrfmiddlewaretoken'})
            print(f.attrs)
            print(f' {inp}')

```

and the output:

```auto
{'id': 'frm-login-modal', 'action': '/accounts/login/', 'method': 'post', 'enter-buttons': '#signin-buttons button.btn-primary'}
 None
{'class': ['rdc-vanilla'], 'action': '/accounts/external', 'method': 'post'}
 <input name="csrfmiddlewaretoken" type="hidden" value="zJjibteg6bAMX6zTREudDYjW5eIf9le16WohD73ipDhkuEy9C5Lzb1FRh1yuspDf"/>
{'id': 'frm-verify-or-backup', 'action': '/accounts/verify', 'method': 'post', 'class': ['flex-grow-1']}
 <input name="csrfmiddlewaretoken" type="hidden" value="zJjibteg6bAMX6zTREudDYjW5eIf9le16WohD73ipDhkuEy9C5Lzb1FRh1yuspDf"/>
{'id': 'frm-signup-modal', 'action': '/accounts/signup/', 'method': 'post'}
 <input name="csrfmiddlewaretoken" type="hidden" value="zJjibteg6bAMX6zTREudDYjW5eIf9le16WohD73ipDhkuEy9C5Lzb1FRh1yuspDf"/>

```

What could cause only the first instance of the tag to be rendered differently? Could it be something to do with the `LazyObject` used to return the token on demand? I’m using Django 5.1.5.

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [February 6, 2025, 12:15am UTC](https://forum.djangoproject.com/t/csrf-token-not-rendering-only-for-first-instance-in-a-template/38582/2 "2025-02-06T00:15:45Z")

</div>

Where is the HTML code of your forms? Especially your first form.  
Is it like this?

```auto
<form ...>
    {% csrf_token %}
    <!-- other form fields -->
</form>

```

---

<div class="post-metadata">

**Author:** ![vsajip](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vsajip/32/14143_2.png) [@vsajip](https://forum.djangoproject.com/u/vsajip)\
**Post date:** [February 9, 2025, 6:14am UTC](https://forum.djangoproject.com/t/csrf-token-not-rendering-only-for-first-instance-in-a-template/38582/3 "2025-02-09T06:14:51Z")

</div>

> [@anefta](#):
>
> Is it like this?

Yes. But I figured out the problem - an unrelated problem was causing the `csrf_token` tag to return an empty string. Fixed now, thanks.
