我正在创建一个购物车作为我正在构建的应用程序的一部分。 我正在尝试找出如何将表单提交添加到字典中(我想这就是我需要做的)。 例如,这就是页面的样子(这只是测试数据)。你知道吗
单击add按钮后,我希望项目名称和价格填充到定价表右侧的Orders表中(开始)。添加完所有订单后,我会单击Order按钮,这样就可以使用sqlalchemy将添加的项目按某种类型的列表排序到数据库中。现在我强烈地感觉到,在使用add按钮提交表单时,表单需要添加到字典中,这可能是错误的。我只是不知道如何保存那本词典,以及词典应该存放在哪里?这是我现在的代码。你知道吗
路线.py 我尝试将dictionary with放入route函数中,但每次提交时都会创建一个实例。所以没有什么东西真的被保存到字典里。你知道吗
@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')
表单.py 不确定是否需要实际形式的函数将值添加到字典中
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
class AddEquipmentForm(FlaskForm):
Type = StringField('Type of Equipment',DataRequired())
Price = StringField('Price',DataRequired())
submit = SubmitField('Add equipment')
class OrderEquipmentForm(FlaskForm):
Type = StringField()
Price = StringField()
Order = SubmitField('Order')
# Should Dictionary go here?
# Not sure
def dict():
dict = {}
dict[Type] = Price
设备.html 如果需要字典,我想在Orders表中循环字典的元素。你知道吗
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col-6-sm">
<h1>Pricing</h1>
<table class='border'>
<thead class='border'>
<th style="width:200px;">Equipment</th>
<th style="width:200px; text-align:center;">Price</th>
<th></th>
</thead>
{% for quip in eq %}
<form method="post">
{{ form.hidden_tag() }}
<tr class ='border'>
<td>{{ quip }}</td>
<td style="text-align:center;"> <strong>${{ eq[quip] }}</strong></td>
<!-- Here I'm adding StringFields from the form but hiding them so they aren't displayed so I can submit the data somehow, hopefully to a dictionary. -->
<td style="display:none;">{{ form.Type(value=quip)}}</td>
<td style="display:none;">{{ form.Price(value=eq[quip]) }}</td>
<td><button class='btn btn-primary' type="submit">Add</button></td>
</tr>
</form>
{% endfor %}
</table>
</div>
<div class="col-6-sm">
<h1>Orders</h1>
<table>
<!-- This is where a loop of the dictionary elements of the items added would go -->
<tr>
<td></td>
<td></td>
</tr>
<button style='float:right' type="button" name="button" class='btn btn-info'>Order</button>
</table>
</div>
</div>
{% endblock %}
目前没有回答
相关问题 更多 >
编程相关推荐