从瓶中的for循环中获取请求命令
get request commands out of a for loop in bottle
我要编写心算程序。因此,我想在一个页面上展示不同的练习。随机练习来自一个自创的功能。为了比较答案,我想将用户输入('value')输入到 json 文件中。问题是 for 循环,因为它只是将最后输入的答案写入文件。也许请求命令卡在那里...
我试图在 for 循环下安排输入框,但那不是我想要的样子。
这是它现在的样子的照片:
瓶码:
@route('/excercises')
def excercise():
ex=addsub()
'''addsub is the function for the random excercises and gaves back a bunch of arrays'''
return template('tgtry', ex=ex)
@route('/excercises', method='POST')
def proof_excercise():
with open('addsub.json', 'r') as jsonFile:
a=json.loads(jsonFile.read())
ax=[]
for row in a:
value = request.forms.get('value')
num={"user_ans": value}
ax.append(num)
with open('answer.json', 'w') as jsonFile:
jsonFile.write(json.dumps(ax, indent = 4,sort_keys = False, ensure_ascii=False))
模板:tgtry.tpl
<form action="/excercises" method="post">
<table>
%for row in ex:
<tr>
<td> {{row['ex']}}. </td>
<td> {{row['numb']}} </td>
<td> {{row['sign']}} </td>
<td> {{row['numbb']}} </td>
<td> {{row['signn']}} </td>
<td> {{row['numbbb']}} </td>
<td> {{row['signnn']}} </td>
<td><input name = 'value' type="number" size='12'></td>
</tr>
%end
</table>
<p><input value="proof answer" type="submit"></p>
</form>
欢迎来到 Stack Overflow!
无论如何,我可以解决您提出的紧迫问题:
The problem is the for loop because it just write the last entered answer into the file.
但请注意,您的设计在其他方面存在缺陷(@Blorgbeard 和@9000 已在评论中指出)。
因为您有一个 multi-valued 表单输入,您应该使用 getall
,而不是 get
来检索它的值。
values = request.forms.getall('value') # returns a list
参考:https://bottlepy.org/docs/dev/api.html#bottle.MultiDict.getall
我要编写心算程序。因此,我想在一个页面上展示不同的练习。随机练习来自一个自创的功能。为了比较答案,我想将用户输入('value')输入到 json 文件中。问题是 for 循环,因为它只是将最后输入的答案写入文件。也许请求命令卡在那里...
我试图在 for 循环下安排输入框,但那不是我想要的样子。
这是它现在的样子的照片:
瓶码:
@route('/excercises')
def excercise():
ex=addsub()
'''addsub is the function for the random excercises and gaves back a bunch of arrays'''
return template('tgtry', ex=ex)
@route('/excercises', method='POST')
def proof_excercise():
with open('addsub.json', 'r') as jsonFile:
a=json.loads(jsonFile.read())
ax=[]
for row in a:
value = request.forms.get('value')
num={"user_ans": value}
ax.append(num)
with open('answer.json', 'w') as jsonFile:
jsonFile.write(json.dumps(ax, indent = 4,sort_keys = False, ensure_ascii=False))
模板:tgtry.tpl
<form action="/excercises" method="post">
<table>
%for row in ex:
<tr>
<td> {{row['ex']}}. </td>
<td> {{row['numb']}} </td>
<td> {{row['sign']}} </td>
<td> {{row['numbb']}} </td>
<td> {{row['signn']}} </td>
<td> {{row['numbbb']}} </td>
<td> {{row['signnn']}} </td>
<td><input name = 'value' type="number" size='12'></td>
</tr>
%end
</table>
<p><input value="proof answer" type="submit"></p>
</form>
欢迎来到 Stack Overflow!
无论如何,我可以解决您提出的紧迫问题:
The problem is the for loop because it just write the last entered answer into the file.
但请注意,您的设计在其他方面存在缺陷(@Blorgbeard 和@9000 已在评论中指出)。
因为您有一个 multi-valued 表单输入,您应该使用 getall
,而不是 get
来检索它的值。
values = request.forms.getall('value') # returns a list
参考:https://bottlepy.org/docs/dev/api.html#bottle.MultiDict.getall