【发布时间】:2019-10-29 12:43:08
【问题描述】:
@app.route(
'/cars/<int:car_shop_id>/menu/new/', methods=['GET', 'POST'])
def newCar(car_shop_id):
if 'username' not in login_session:
return redirect('/login')
session = connect_to_database()
if request.method == 'POST':
newItem = Cars(name=request.form['name'],
description=request.form['description'],
price=request.form['price'],
car_shop_id=car_shop_id)
session.add(newItem)
session.commit()
flash("Car has been added")
return redirect(url_for('Car_shopMenu', car_shop_id=car_shop_id))
else:
return render_template('newcars.html', car_shop_id=car_shop_id)
【问题讨论】:
-
不会执行同一块中
return以下的任何代码。 -
您对
session的定义是第一个if块的一部分。将其降低到函数体的级别。 -
@MisterMiyagi 我认为它在定义会话时有效,如果范围但我出现一个新错误:AttributeError: 'sqlite3.Connection' object has no attribute 'add'
-
@YosefHamdy 好吧,
sqlite3.Connection没有add方法。是什么让您认为它确实有效?