python - 如何使用 Python 和 Flask 摆脱 500 内部服务器错误?
问题描述
我的网站出现内部服务器错误。当您转到结果页面时,它会抛出 500 Internal Server Error。我不太确定为什么。它说我收到“KeyError:'test'”。
这是Python中的代码:
@app.route('/results/')
def results():
votes = {}
for f in poll_data['fields']:
votes[f] = 0
f = open(file, 'r+')
for line in f:
voted = line.rstrip("\n")
votes[voted] += 1
return render_template('results.html', data=poll_data, votes=votes)
这是更多代码:
file = 'data0.txt'
@app.route('/')
def home():
return render_template('home.html', data = poll_data)
@app.route('/poll')
def poll():
vote = request.args.get('field')
out = open(file, 'a+')
out.write( vote + '\n' )
out.close()
return render_template('thankyou.html', data = poll_data)
@app.route('/results/')
def results():
votes = collections.defaultdict(int)
for f in poll_data['fields']:
votes[f] = 0
f = open(file, 'r+')
for line in f:
vote = line.rstrip("\n")
votes[vote] += 1
return render_template('results.html', data=poll_data, votes=votes)
@app.route('/contact/')
def contact():
return render_template('contact.html')
@app.route('/helpfullinks/')
def helpfullinks():
return render_template('helpfullinks.html')
if __name__ == "__main__":
app.run(debug=True)
解决方案
根据您的屏幕截图,问题是votes
没有键作为voted
. 如果您更改votes
为 -votes=Counter()
或votes=defaultdict(int)
(都从应该解决的集合中导入)
推荐阅读
- swift - NSDocument 的菜单栏没有出现
- c# - 无法在 Azure Devops 中打开附加到工作项的 Excel 文件
- mysql - 选择两列的值在数组中的行
- r - 如何从列线图中提取预测点分数并将其作为协变量包含在我的数据框中?
- matlab - 我正在寻找一个单行操作来创建这个矩阵:
- javascript - Html/css 侧边栏没有正确显示在页面右侧
- postgresql - psql 无法连接到服务器,用户 postgres 的密码验证失败
- c# - 如何更改多个文件的路径c#
- javascript - 日期到字符串的转换在 JavaScript 中显示不正确的格式
- c - C语言中的for循环