Card.template() does not work when called in card_will_show hook in template editor

I develop a: an addon for writing Hanzi in Anki. In the newest version of Anki, I think around 2.1.30, card.template()called by acard_will_show` hook in the template editor starts throwing:

Traceback (most recent call last):
  File "aqt\", line 43, in handler
  File "aqt\", line 468, in _renderPreview
  File "aqt\", line 787, in __call__
  File "D:\AnkiData\addons21\maobi\", line 31, in my_hook
  File "anki\", line 155, in template
  File "anki\", line 146, in note_type
  File "anki\", line 142, in note
  File "anki\", line 309, in getNote
  File "anki\", 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):


Users reported a bug on my Github, 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.

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.

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

1 Like