首页 > 解决方案 > 如何使用 html post 提交动态表格

问题描述

我有一个 html 表,它基本上来自我的 django 模型

<form action="{% url 'checkout' %}" method="POST">
    <table id="table" class="table">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Item</th>
          <th scope="col">Quntity</th>
          <th scope="col">Price</th>
        </tr>
      </thead>
      <tbody>
        {% csrf_token %}
        {% for item in cartitems %}
        <tr>
          <th scope="row">{{ forloop.counter }}</th>
          <td>{{ item.name }}</td>
          <td data-count="1" id="counter_{{ forloop.counter }}">
            <button onclick="minus({{ forloop.counter }})" id="minusButton" data-count="1"
              class="btn btn-default btn-xs">
              <i class="fas fa-minus mr-2"></i>
            </button>
            1
            <button onclick="plus({{ forloop.counter }})" id="plusButton" data-count="1" class="btn btn-default btn-xs">
              <i class="fas fa-plus ml-2"></i>
            </button>
          </td>
          <td data-price="{{ item.price }}" id="counter_{{ forloop.counter }}">
            {{ item.price }}
          </td>
        </tr>
        {% endfor %}
      </tbody>
      <tfoot>
        <tr>
          <td>Total Price</td>
          <td></td>
          <td></td>
          <td id='totalPrice'></td>
        </tr>
      </tfoot>
    </table>
</div>
<div>
  <button type="submit" class="btn btn-default btn-lg">Proceed To Checkout</button>
</div>
</form>

我表中的数量字段来自用户,所以我想将表中的所有数据发布到 django,请让我知道是否有任何简单的方法可以做到这一点。现在我的表格没有做任何事情。

我知道您可以通过 ajax 发布 quatity 字段,但我不希望这样,因为用户可能还会从购物车中删除一些商品

标签: htmldjangopost

解决方案


推荐阅读