首页 > 解决方案 > 如何在提交时将表单添加到字典

问题描述

我正在创建一个购物车作为我正在构建的应用程序的一部分。我正在尝试弄清楚如何将表单提交添加到字典中(我认为这是我需要做的)。例如,这就是页面的样子(这只是测试数据)。

大车单击添加按钮后,我希望将项目名称和价格填充到定价表右侧的订单表中(开始)。添加完所有订单后,我将单击“订单”按钮,这将使用 sqlalchemy 将某种类型列表中添加的项目排序到数据库中。现在我感觉很强烈,我可能错了,在使用添加按钮提交表单时,需要将表单添加到字典中。我只是不知道如何保存该字典以及该字典应该存储在哪里?这是我现在的代码。

routes.py 我尝试将字典放在路由函数中,但每次提交时都会创建一个实例。所以没有任何东西真正被保存到字典中。

@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')

Forms.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

device.html 如果需要字典,我想在订单表中循环字典的元素。

{% 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 %}

标签: pythondictionaryflaskflask-sqlalchemyflask-wtforms

解决方案


推荐阅读