# Card.template() does not work when called in card\_will\_show hook in template editor

**URL:** <https://forums.ankiweb.net/t/card-template-does-not-work-when-called-in-card-will-show-hook-in-template-editor/2798>\
**Category:** Add-ons\
**Created:** [August 29, 2020, 1:25pm UTC](https://forums.ankiweb.net/t/card-template-does-not-work-when-called-in-card-will-show-hook-in-template-editor/2798 "2020-08-29T13:25:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandalf](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jandalf](https://forums.ankiweb.net/u/jandalf)\
**Post date:** [August 29, 2020, 1:25pm UTC](https://forums.ankiweb.net/t/card-template-does-not-work-when-called-in-card-will-show-hook-in-template-editor/2798/1 "2020-08-29T13:25:06Z")

</div>

Hello,  
I develop a: [an addon for writing Hanzi in Anki](https://github.com/jcklie/anki-maobi). In the newest version of Anki, I think around 2.1.30, card.template()`called by a`card\_will\_show` hook in the template editor starts throwing:

```
Traceback (most recent call last):
  File "aqt\progress.py", line 43, in handler
  File "aqt\clayout.py", line 468, in _renderPreview
  File "aqt\gui_hooks.py", line 787, in __call__
  File "D:\AnkiData\addons21\maobi\ __init__.py", line 31, in my_hook
    sys.stderr.write(str(card.template()))
  File "anki\cards.py", line 155, in template
  File "anki\cards.py", line 146, in note_type
  File "anki\cards.py", line 142, in note
  File "anki\collection.py", line 309, in getNote
  File "anki\notes.py", line 37, in __init__
TypeError: 'NoneType' object is not subscriptable

```

Minimal addon for testing:

```
import sys
from aqt import gui_hooks

def my_hook(html, card, context):
    sys.stderr.write(str(card))
    sys.stderr.write("\n")
    sys.stderr.write(str(card.template()))
    sys.stderr.write("\n")

gui_hooks.card_will_show.append(my_hook)

```

Users reported a bug [on my Github](https://github.com/jcklie/anki-maobi/issues/36), there is a more detailed description. It worked before so I assume that is a regression in Anki itself. I looked in the Anki source but have no idea why the error happens, as it is related to some database access which I am not familiar with.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/4/4aaa7db1f3f4d74688995ca4b48c6c1b6734dce5.png)

---

<div class="post-metadata">

**Author:** ![jandalf](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jandalf](https://forums.ankiweb.net/u/jandalf)\
**Post date:** [August 29, 2020, 2:29pm UTC](https://forums.ankiweb.net/t/card-template-does-not-work-when-called-in-card-will-show-hook-in-template-editor/2798/2 "2020-08-29T14:29:44Z")

</div>

If I would guess then it looks like `ephemeral_card_for_rendering` does not set all the required fields and so the card cannot be loaded properly.

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [August 30, 2020, 12:54am UTC](https://forums.ankiweb.net/t/card-template-does-not-work-when-called-in-card-will-show-hook-in-template-editor/2798/4 "2020-08-30T00:54:15Z")

</div>

Thanks for the report, this will be fixed in the next release.
