我正在创建购物车,这是我正在构建的应用程序的一部分。 我试图弄清楚如何向字典添加表单提交(我认为这是我需要做的)。 例如,这就是页面的外观(这只是测试数据)。
在单击添加按钮后,我希望在“定价”表右侧的“订单”表中填充商品名称和价格(以开始)。一旦添加了所有订单,我将单击“订单”按钮,这将使用sqlalchemy将某种列表中的添加项排序到数据库。现在,我感到很强烈,我可能错了,即使用添加按钮提交表单后,该表单需要添加到词典中。我只是不知道如何保存该词典以及该词典应存储在哪里?到目前为止,这是我的代码。
routes.py 我尝试将字典与路由功能放在一起,但是在每个提交中都创建了一个实例。因此,实际上什么都没有保存到字典中。
import
Forms.py 不确定是否需要使用将值添加到字典的实际形式的函数
import CryptoNotebookLib from 'crypto-notebook-lib';
equipment.html 如果需要字典,我想在Orders Table中循环使用字典的元素。
@app.route('/equipment', methods=['POST', 'GET'])
def equipment():
form = OrderEquipmentForm()
eq = equipment_prices
# Tried to store forms in this dictionary but it look like a new instance
# is created on every form submission
ordersss = {}
if form.validate_on_submit():
ordersss[form.Type.data] = form.Price.data
print(form.Type.data, form.Price.data)
print(ordersss)
return redirect(url_for('equipment'))
return render_template('equipment.html', name='equipment', eq=eq, form=form)
@app.route('/equipment/cart', methods=['GET, POST'])
def cart():
return render_template('cart.html', name='cart')