So in the controller of my app in default.py I've defined this:
from database_handler import getQuestion
def questionnaire():
questionDict = getQuestion(1, 'english') # placeholder attributes for the actual stuff
return dict(data=questionDict)
the module it refers to is following:
def getQuestion(id: int, language: str) -> dict:
questionDict = {
'question': "What also floats in water?",
'answer_1': "Bread",
'answer_2': "Apples",
'answer_3': "Very small rocks",
'answer_4': "Churches",
'answer_5': "A duck",
}
return questionDict
The view questionnaire.html is:
{{extend 'layout.html'}}
<div>
<center>
<table>
<thead>
<tr>
<th></th>
<th>{{=data['answer_1']}}</th>
<th>{{=data['answer_2']}}</th>
<th>{{=data['answer_3']}}</th>
<th>{{=data['answer_4']}}</th>
<th>{{=data['answer_5']}}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{=data['question']}}</td>
<td>RADIO1</td>
<td>RADIO2</td>
<td>RADIO3</td>
<td>RADIO4</td>
<td>RADIO5</td>
</tr>
</tbody>
</table>
</center>
</div>
... and I get a <class 'KeyError'> 'answer_1' when I load the page.
Why the heck am I getting a KeyError? I assume it's got to do with how I pass data from the controller to the view, but how should do this for it to actually work?
UPDATE 1:
So if I change the view to this:
{{extend 'layout.html'}}
<div>
<center>
<table>
<thead>
<tr>
<th></th>
<th>{{=answer_1}}</th>
<th>{{=answer_2}}</th>
<th>{{=answer_3}}</th>
<th>{{=answer_4}}</th>
<th>{{=answer_5}}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{=question}}</td>
<td>RADIO1</td>
<td>RADIO2</td>
<td>RADIO3</td>
<td>RADIO4</td>
<td>RADIO5</td>
</tr>
</tbody>
</table>
</center>
</div>
and my controller to this:
def questionnaire():
#questionDict = database_handler.getQuestion(1, 'english') # placeholder attributes for the actual stuff
questionDict = {
'question': "What also floats in water?",
'answer_1': "Bread",
'answer_2': "Apples",
'answer_3': "Very small rocks",
'answer_4': "Churches",
'answer_5': "A duck",
}
return dict(question = questionDict['question'],
answer_1 = questionDict['answer_1'],
answer_2 = questionDict['answer_2'],
answer_3 = questionDict['answer_3'],
answer_4 = questionDict['answer_4'],
answer_5 = questionDict['answer_5'])
that works, but if replace the questionDict defining in the controller with
questionDict = database_handler.getQuestion(1, 'english') # placeholder attributes for the actual stuff
given that the module database_handler is still (in its entirety) the following:
def getQuestion(id: int, language: str) -> dict:
questionDict = {
'question': "What also floats in water?",
'answer_1': "Bread",
'answer_2': "Apples",
'answer_3': "Very small rocks",
'answer_4': "Churches",
'answer_5': "A duck",
}
return questionDict
it creashes with the traceback
Traceback (most recent call last): File "C:\Users\JOENSJOO\Documents\GitHub\Hoitopolku\web2py\gluon\restricted.py", line 219, in restricted exec(ccode, environment) File "C:/Users/JOENSJOO/Documents/GitHub/Hoitopolku/web2py/applications/bref/controllers/default.py", line 84, in File "C:\Users\JOENSJOO\Documents\GitHub\Hoitopolku\web2py\gluon\globals.py", line 430, in self._caller = lambda f: f() File "C:/Users/JOENSJOO/Documents/GitHub/Hoitopolku/web2py/applications/bref/controllers/default.py", line 28, in questionnaire answer_1 = questionDict['answer_1'], KeyError: 'answer_1'
I mean srsly, WTF? I can't pass a dict from a module into the controller using normal python syntax?