如何在不覆盖先前数据的情况下将一个表中的选定数据临时保存到另一个表?

问题描述 投票:0回答:1

我有一个html表,我需要在其中将一行数据从该表转移或复制到另一个具有与我在数据库中称为cart的结构的表中。为此,当我单击给定行的按钮(+)时,希望在购物车表中添加相应的数据。

Data from Product model

这是我的模板

 <form type="post" action="" style="margin: 0" >
        <label for="code" class="nav-link">Référence </label>
        <div class="col-sm-9">
            <input  id="productCode" type="text" name="productCode"  placeholder="Entez le code du produit ..." onkeyup="myFunction()">
        </div>  
    </form>

    <table class="table table-bordered" id="productsTable" width="400">
        <thead>
            <tr>
                <th>ID</th>
                <th width="10%">Code</th>
                <!-- <th width="16%">Catégorie</th> -->
                <th width="50%">Libéllé</th>
                <!-- <th width="12%">Marque</th> -->
                <th  width="11%">Date entrée </th>
                <th width="11%">Qté initiale </th>
                <!-- <th width="12%">Quantity </th> -->
                <!-- <th width="12%">Qtité finale </th> -->
                <th>PU</th>
                <!-- <th>Statut</th> -->
                <th style="align-self: center;">Actions</th>
            </tr>
        </thead>
        <tbody>
        {% if products|length < 1 %}
            <tr>
            <td colspan="20" class="text-center">Aucune donnée trouvée, veuillez ajouter quelques données!</td>
            </tr>
        {% else %}
            {% for product in products %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ product.code }}</td>
                    <!-- <td>{{ product.category }}</td> -->
                    <td>{{ product.name }}</td>
                    <!-- <td>{{ product.brand }}</td> -->
                    <td>{{ product.date_entry }}</td>
                    <td>{{ product.quantity_entry }}</td>
                    <!-- <td>{{ product.quantity }}</td> -->
                    <!-- <td>{{ product.final_stock }}</td> -->
                    <td>{{ product.unit_price }}</td>
                    <!-- <td>{{ product.status }}</td> -->
                    <!-- <td><label {% if product.status == '1' %}class="badge badge-success" {% else %} class="badge badge-danger" {% endif %}>{{ product.get_status_display }}</label></td> -->
                    <td>
                        <!-- <button class="btn btn-danger" id="btn-delete" data-url="#" data-toggle="modal"
                                    data-target="#removeCategoryModal"></button> -->
                            <!-- <span class="glyphicon glyphicon-trash"> --><a href="#"><i class="fa fa-trash" style="color:red"></i></a>
                            <!-- Supprimer -->

                        <!-- <button class="btn btn-info show-form-delete" id="btn-update" data-url="#" data-taraget="#btn-update" data-toggle="modal"></button> -->
                           <!--  <span class="glyphicon glyphicon-pencil"></span> --><a href="#"><i class="fa fa-pencil"></i></a>
                            <!-- Editer -->
                        <a href="#"><i class="fa fa-plus-square"></i></a>

                    </td>
                </tr>
            {% endfor %}
        {% endif %}
        </tbody>
    </table>

    <script type="text/javascript">
        function myFunction() {
                var input, filter, table, tr, td, i, txtValue;
                input = document.getElementById("productCode");
                filter = input.value.toUpperCase();
                table = document.getElementById("productsTable");
                tr = table.getElementsByTagName("tr");
                for (i = 0; i < tr.length; i++) {
                    td = tr[i].getElementsByTagName("td")[1];
                    if (td) {
                        txtValue = td.textContent || td.innerText;
                        if (txtValue.toUpperCase().indexOf(filter) > -1) {
                            tr[i].style.display = "";
                        } else {
                            tr[i].style.display = "none";
                        }
                    }       
                }
            }
    </script>

这是显示产品列表的视图

def products_list(request):
    products_lists = Product.objects.all()
    print(products_lists)
    html = render_to_string('modules/tables_products.html', {"products": products_lists})
    return JsonResponse({"message": "Ok", "html": html})

注意,我有一个产品模型,其内容在上表中,而购物车模型将临时从表中所选的每一行中获取数据。

[请协助我获得转移意见。

django python-3.x django-templates postgresql-9.4
1个回答
0
投票

对于电子商务/购物车而言,我建议使用会话来构建购物车。 github上有一个项目示例可以做到这一点。签出cart-code

如果您想了解有关django会话django-sessions的更多信息,则>

© www.soinside.com 2019 - 2024. All rights reserved.