我正在尝试在按下submit时从文本字段中获取表单数据,以便将其放入json格式并作为另一个页面访问json数据,该页面将是localhost:5000/info。每次我试图用request.form.get('')访问数据时,它只返回一个空字典。我阅读了stackoverflow上的其他帖子,试图找出问题所在,但似乎没有一个解决方案有效。如果可能的话,我想避免使用模板或模块以外的烧瓶。在
这是我的python代码from flask import Flask, request, jsonify, redirect
app = Flask(__name__)
numCarsEast = None
numCarsWest = None
numCarsSouth = None
numCarsNorth = None
@app.route('/info.json', methods=['GET', 'POST'])
def getInfo():
if request.method == 'GET':
lightEast = {}
lightWest = {}
lightNorth = {}
lightSouth = {}
intersection1 = {}
lightEast['cars'] = numCarsEast
lightWest['cars'] = numCarsWest
lightNorth['cars'] = numCarsNorth
lightSouth['cars'] = numCarsSouth
intersection1['eastLight'] = lightEast
intersection1['westLight'] = lightWest
intersection1['northLight'] = lightNorth
intersection1['southLight'] = lightSouth
return jsonify(intersection=intersection1)
@app.route('/cars', methods=['GET', 'POST'])
def cars():
if request.method == 'POST':
numCarsEast = request.form.get('eastLightInt1', None)
numCarsWest = request.form.get('westLightInt1', None)
numCarsNorth = request.form.get('northLightInt1', None)
numCarsSouth = request.form.get('southLightInt1', None)
print(str(numCarsEast) + ' east')
print(str(numCarsWest) + ' west')
print(str(numCarsNorth) + ' north')
print(str(numCarsSouth) + ' south')
return 'done'
return open('./carForm.html').read()
if __name__ == '__main__':
app.debug = True
app.run()
这是HTML
^{pr2}$