Cards not created properly when the model template contains {{^FieldName}}
Description Cards with conditional replacements of the form {{^FieldName}} are not handled correctly. I think I have tracked it to an error in the line rendered = chevron.render(template['qfmt'], field_values) in model.py that is, it does no render the same as anki.
Note that if you import the apkg generated by genanki into anki and then run "Check Database" anki will then create the correct cards.
To reproduce, add the following to test_genanki.py
TEST_MODEL_MUST_HAVE_HINT = genanki.Model(
456789, 'with hint',
fields=[{'name': 'Question'}, {'name': 'Hint'}, {'name': 'Answer'}],
templates=[
{
'name': 'card1',
'qfmt': '{{#Hint}}{{Question}}<br>Hint: {{Hint}}{{/Hint}}',
'afmt': '{{Answer}}',
},
],
)
TEST_MODEL_MUST_NOT_HAVE_HINT = genanki.Model(
456789, 'with hint',
fields=[{'name': 'Question'}, {'name': 'Hint'}, {'name': 'Answer'}],
templates=[
{
'name': 'card1',
'qfmt': '{{^Hint}}{{Question}}<br>Hint: {{Hint}}{{/Hint}}',
'afmt': '{{Answer}}',
},
],
)
def test_notes_generate_cards_based_on_req__must_have_hint(self):
# only n2 has a hint and will generate one card
n1 = genanki.Note(model=TEST_MODEL_MUST_HAVE_HINT, fields=['capital of California', '', 'Sacramento'])
n2 = genanki.Note(model=TEST_MODEL_MUST_HAVE_HINT, fields=['capital of Iowa', 'French for "The Moines"', 'Des Moines'])
assert len(n1.cards) == 0
assert len(n2.cards) == 1
assert n2.cards[0].ord == 0
def test_notes_generate_cards_based_on_req__must_not_have_hint(self):
# n2 has a hint so only n1 will generate one card
n1 = genanki.Note(model=TEST_MODEL_MUST_NOT_HAVE_HINT, fields=['capital of California', '', 'Sacramento'])
n2 = genanki.Note(model=TEST_MODEL_MUST_NOT_HAVE_HINT, fields=['capital of Iowa', 'French for "The Moines"', 'Des Moines'])
assert len(n1.cards) == 1
assert n2.cards[0].ord == 1
assert len(n2.cards) == 0
# The following commented lines make the test pass, but they are wrong.
# assert len(n1.cards) == 1
# assert len(n2.cards) == 1
'name': 'Card 1', 'qfmt': ''' <div class="front">{{edit:Front}}</div> ''', 'afmt': ''' <div class="front">{{edit:FrontSide}}</div> <hr id="answer"> <div class="back">{{edit:Back}}</div> {{#Notes}} <hr> <div class="notes"> <b>Notes:</b><br> {{edit:Notes}} </div> {{/Notes}}
'
it's not about conditionals
the problem is about missing an essional part
<
div class="front">{{edit:FrontSide}} # needed so it correctly renders to anki format
# used for card browser to display it without the question
I prosennaly ran into this problem and kept using it since then for about 4 months then figured out Alhamdulillah I really hope it works for you
you don't need to check database and change their deck from "default"