因此,我正在尝试制作一个预算应用程序,主要是为了自己的python / flask实践。该应用程序可以在用户以“收入”形式输入值并将其转移到下一页的情况下工作。
当表单数据移至下一个URL时,它会被完美保存,但是如果我尝试在以后的URL中调用它,则会收到BadRequestKeyError,并且页面似乎再也找不到我的表单数据。 / p>
代码如下:
from flask import Flask, render_template, request, session
from datetime import date
app = Flask(__name__)
app.secret_key = "Nash"
@app.route('/', methods=['GET'])
def entry():
return render_template('entry.html',
the_title='Smith Family Budgeting')
@app.route('/income', methods=['GET'])
def income():
session['income'] = ''
return render_template('income.html')
@app.route('/housing', methods=['POST'])
def housing():
if request.method == 'POST':
session['housing'] = ''
session['income'] = request.form["income"]
return render_template('housing.html', the_income=session['income'])
@app.route('/transportation', methods=['POST'])
def transport():
if request.method == 'POST':
income = session["income"]
session['housing'] = request.form["housing"]
session['leftover'] = float(income) - float(session['housing'])
return render_template('trans.html', leftover=session['leftover'])
@app.route('/food', methods=['POST'])
def food():
if request.method == 'POST':
session['gas'] = request.form['gas']
session['leftover'] = int(session['leftover'] - int(session['gas']))
return render_template('food.html', leftover=session['leftover'])
@app.route('/utilities', methods=['POST'])
def utilities():
if request.method == 'POST':
session['food'] = request.form['food']
session['leftover'] = float(session['leftover']) - float(session['food'])
return render_template('utilities.html',
leftover=session['leftover'])
@app.route('/insurance', methods=['POST'])
def insurance():
if request.method == 'POST':
session['bills'] = float(request.form['gas_bill']) + float(request.form['electric_bill']) + float(request.form['internet_bill']) + float(request.form['city_bill'])
session['leftover'] = float(session['leftover']) - float(session['bills'])
return render_template('insurance.html', leftover=session['leftover'])
@app.route('/entertainment', methods=['POST'])
def fun():
if request.method == 'POST':
session['leftover'] = float(session['leftover']) - float(request.form['insurance']) -float(request.form['life'])
return render_template('entertainment.html', leftover=session['leftover'])
@app.route('/services', methods=['POST'])
def services():
if request.method == 'POST':
session['leftover'] = float(session['leftover']) - float(request.form['derek']) - float(request.form['taylor']) - float(request.form['outings']) - float(request.form['dates'])
return render_template('services.html', leftover=session['leftover'])
@app.route('/health', methods=['POST'])
def health():
if request.method == 'POST':
session['services'] = float(request.form['hulu']) + float(request.form['spotify']) + float(request.form['amc']) + float(request.form['amazon'])
session['leftover'] = session['leftover'] - session['services']
return render_template('health.html',
leftover=session['leftover'])
@app.route('/savings', methods=['POST'])
def savings():
if request.method == 'POST':
session['health'] = float(request.form['health'])
session['leftover'] = session['leftover'] - session['health']
return render_template('savings.html',
leftover=session['leftover'])
@app.route('/results', methods=['POST'])
def results():
if request.method == 'POST':
housing = request.form['housing']
session['savings_and_givings'] = float(request.form['savings']) - float(request.form['giving'])
session['leftover'] = session['leftover'] - session['savings_and_givings']
today = date.today()
return render_template('results.html',
leftover=session['leftover'],
date=today,)
app.run(debug = True)
错误回溯:
Traceback (most recent call last):
File "/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "budget/budget.py", line 84, in results
housing = request.form['housing']
File "/python3.7/site-packages/werkzeug/datastructures.py", line 443, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser(or proxy) sent a request that this server could not understand.
KeyError: 'housing'
为什么会发生这种情况?预先感谢
答案 0 :(得分:1)
尝试从表单中不存在的表单获取值。在这里housing = request.form['housing']
使用session['housing'] = request.form["housing"]
将数据存储在flask session中时,数据将存储在用户的浏览器cookie中,并且在用户重新启动浏览器时将被清除。
@app.route('/results', methods=['POST'])
def results():
if request.method == 'POST':
housing = session['housing']
session['savings_and_givings'] = float(request.form['savings']) - float(request.form['giving'])
session['leftover'] = session['leftover'] - session['savings_and_givings']
today = date.today()
return render_template('results.html',
leftover=session['leftover'],
date=today,)
如果值不存在,请使用get
提供默认值
session['income'] = request.form.get("income", 0)
使用session.clear()
清除用户Cookie中的所有数据
@app.route('/', methods=['GET'])
def entry():
session.clear()
return render_template('entry.html',
the_title='Smith Family Budgeting')