使用django-autocomplete-light添加更多字段时出错

时间:2018-02-25 00:16:46

标签: django autocomplete django-forms django-autocomplete-light

我遇到了问题,我正在使用2个库: django-autocomplete-light django-dynamic-formset 。 2人非常擅长做他们的工作。 第一个用于执行自动完成,第二个用于使django formsets是动态的。 当您想要加入这两个问题时,会出现问题。 Image of the problem

创建新字段时,会以这种方式添加。

模板:

{% extends 'base/base.html' %}
{% load static %}
{% block titulo%} Registrar venta {%endblock%}
{% block contenido %}
<div class="col-md-12">
<form method="post">{% csrf_token %}
    <div class="col-md-4 form-group">
    <label class="font-weight-bold" for="{{form.cliente.name}}">{{form.cliente.label}}</label>
    {{form.cliente}}
    </div>
    <h4 class="text-left">Detalle de venta: </h4>
    <div class="table-responsive-sm">
        <table class="table" id="tablaDetalle">
            {{ detalleformset.management_form }}
            <thead class="thead-dark">
                <th>Producto</th>
                <th width="100px">Cantidad</th>
                <th width="115px">Prec.Unit.</th>
                <th width="115px">Subtotal</th>
                <th>Acción</th>
            </thead>
            <tbody>
            {% for form in detalleformset.forms %}
                <tr class="formset_row">
                    {% for field in form.visible_fields %}
                        <td>
                            {# Include the hidden fields in the form #}
                            {% if forloop.first %}
                                {% for hidden in form.hidden_fields %}
                                    {{ hidden }}
                                {% endfor %}
                            {% endif %}
                            {{ field.errors.as_ul }}
                            {{ field }}
                        </td>
                    {% endfor %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
        </div>
        <div class="row justify-content-md-end">
        <div class="col-md-2">
        <label class="font-weight-bold" for="{{form.total.name}}">{{form.total.label}}</label>
        {{form.total}}
        </div>
        </div>
        <div class="form-group">
        <label class="font-weight-bold" for="{{form.descripcion.name}}">{{form.descripcion.label}}</label>
        {{form.descripcion}}
        </div>
        <div class="col-md-4 offset-md-4">
          <button class="btn btn-block btn-lg btn-primary" type="submit"><span><i class="fa fa-shopping-cart"></i>
          </span>Registrar venta</button>
        </div>
    </form>
    </div>
   {% endblock %}
   {% block javascript %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
{{ detalleformset.media }}
<script src="{% static 'js/jquery.formset.js' %}"></script>
<script type="text/javascript">
  $('.formset_row').formset({
        addText: 'Agregar Producto',
        deleteText: 'remover',
        prefix: 'detalleventa'
    });
        $("#tablaDetalle").on("focus keyup", "tr", function(){
          var total = 0;

          var row = $(this).closest("tr");
          var cantidad = parseInt(row.find("input:eq(2)").val());
          var precio = parseFloat(row.find("input:eq(3)").val());
          var subtotal = cantidad * precio;
          row.find("input:eq(4)").val(isNaN(subtotal) ? "" : subtotal.toFixed(2));

            $(".subtotal").each(function () {
                var stval = parseFloat($(this).val());
                total += isNaN(stval) ? 0 : stval;
            });

            $('.delete-row').click(function(){
                var $fila = $(this).parents('tr');
                var valsub = parseFloat($fila.find('input:eq(4)').val());
                new Promise(function(done){
                    total -= isNaN(valsub) ? 0 : valsub;
                    $('.total').val(total.toFixed(2));
                    done();
                })
                .then(function(){
                   $fila.find('input:eq(4)').val(0);
                })
            });

            $('.total').val(total.toFixed(2));
        });
    </script>   
    {% endblock %}

有什么方法可以解决这个问题吗?我正在阅读,而且信息很少

2 个答案:

答案 0 :(得分:1)

我最近在使用django-autocomplete-lightdjango-dynamic-formset插件时遇到了同样的问题。我发现的解决方案是在初始化表单集之前,即在打开{{ detalleformset.media }}标签之前,将<form>移动到。那为我解决了这个问题,并使其不再创建那些多余的不可点击的自动完成字段。

答案 1 :(得分:0)

django-autocomplete-light 3.8.1 版解决了这个问题。您可以在此处阅读发行说明:https://yourlabs.org/posts/2020-10-12-django-autocomplete-light-380-release/

就我而言,我必须先 List<Block>? blocks; ... // you are not sure blocks variable is initialized or not. // block is nullable. final Block? block = blocks?.first; // you are sure blocks variable is initialized. // block is not nullable. final Block block = blocks!.first; 3.5.0 版,然后全新安装 3.8.1 版。它对我有用。