如果我更改select中的选项,则在模板中出现错误消息后,Django select选项将休息

时间:2019-03-15 08:36:15

标签: jquery django

我已经为交货单创建了表单,我正在使用表单formset,

forms.py

  class Delivery_note_transiction_form(forms.Form):
        item = forms.ModelChoiceField(label=False,queryset=Product.objects.none(),widget=Select2Widget(attrs={"class" : "item"}))
        description = forms.CharField(widget=forms.TextInput(attrs={ 'placeholder' : 'optionall','class' : 'description'}),label=False,required=False)
        quantity = forms.IntegerField(widget=forms.NumberInput(attrs={'class' : 'quantity'}),label=False,min_value=0)
        id = forms.CharField(widget=forms.HiddenInput,required=False)

views.py

def delivery_note_creation(request):
    Delivery_note_transiction_form_formset = forms.formset_factory(form=Delivery_note_transiction_form,can_delete=True)

    if request.method == 'POST':
        inv_no = Invoice_max.objects.get(invoice_name='delivery')
        form = Deliver_Note_Main_Modelform(request.POST,initial={'delivery_invoice_no' : inv_no.invoice_no + 1})
        formset = Delivery_note_transiction_form_formset(request.POST,request.FILES)
        if form.is_valid() and formset.is_valid():

            ins = form.save(commit=False)
            ins.author = request.user
            ins.save()

            for transfer in formset:
                Delivery_Note_Transiction.objects.create(item=transfer.cleaned_data['item'],
                                                         description=transfer.cleaned_data['description'],
                                                         quantity=transfer.cleaned_data['quantity'],
                                                         delivery_invoice_no=ins ,
                                                         )

            max_invoice = Invoice_max.objects.get(invoice_name='delivery')
            max_invoice.invoice_no = max_invoice.invoice_no + 1
            max_invoice.save()

            return HttpResponseRedirect(reverse_lazy('delivery note:delivery note home page'))

    else:
        invoice_create = Invoice_max.objects.get_or_create(invoice_name='delivery')
        inv = Invoice_max.objects.get(invoice_name='delivery')
        form = Deliver_Note_Main_Modelform(initial={ 'deliver_date' : datetime.today(), 'delivery_invoice_no' : inv.invoice_no + 1, 'total_quantity':0})
        formset = Delivery_note_transiction_form_formset()
    return render(request,'delivery_note/delivery note creation.html', { 'deliver_main': form,
                                                                         'delivery_transiction' : formset


       })

trmplate.html

{% extends 'home/base.html' %}
{% load crispy_forms_tags %}
{% block title %}

{% endblock title %}

{% block media %}
    {{ deliver_main.media.css }}
    {{ delivery_transiction.media.css}}
{% endblock media %}

{% block content %}


    <div class="w3-container">
        <h1>Delivery Note</h1>

        <form method="post">{% csrf_token %}
            <div class="row">
                {{ deliver_main.errors }}
                {{ deliver_main.non_field_errors }}
                <div class="row">
                    <div class="col-sm-6">{{ deliver_main.customer|as_crispy_field }}</div>
                    <div class="col-sm-6">{{ deliver_main.deliver_date|as_crispy_field }}</div>

                </div>

                <div class="row">
                    <div class="col-sm-6">{{ deliver_main.delivery_address|as_crispy_field }}</div>
                    <div class="col-sm-6">{{ deliver_main.delivery_invoice_no|as_crispy_field }}</div>
                </div>
            </div>

            <table class="w3-table-all w3-border w3-striped">
                <tr>
                    <th class="col-sm-4">Item</th>
                    <th class="col-sm-4">Description</th>
                    <th class="col-sm-4">Quantity</th>
                </tr>
                {{ delivery_transiction.management_form }}
                {% for error in delivery_transiction.errors %}
                    {{ error }}
                {% endfor %}
                {% for error in delivery_transiction.non_form_errors %}
                    {{ error }}
                {% endfor %}

                {% for delivery in delivery_transiction %}
                    <tr class=" delivery_form ">
                        {{ delivery.id }}
                        <td class="col-sm-4">{{ delivery.item|as_crispy_field  }}</td>
                        <td class="col-sm-4">{{ delivery.description|as_crispy_field  }}</td>
                        <td class="col-sm-4">{{ delivery.quantity|as_crispy_field  }}</td>
                    </tr>
                {% endfor %}
            </table>
            <button id="add-row" type="button">Add Row</button>
            <div class="row ">
                {{ deliver_main.description|as_crispy_field }}
                {{ deliver_main.total_quantity|as_crispy_field }}
            </div>


            <input type="submit" value="Create">
        </form>
    </div>





{% endblock content %}

{% block script %}
    <script>
        $("#id_customer").change(function () {
            var url = "{% url "delivery note:product ajax" %}";
            $.ajax({
                url: url,
                success: function (data) {
                    $(".item").html(data);

                    alert(data)
                }
            })
        })
    </script>

    <script>
        {#for set address in delivery#}
        $("#id_customer").change(function () {
            var customer = $("#id_customer option:selected").text();
            var url = "{% url 'saleinvoice:adress ajax' %}";

            {#for set address#}
            $.ajax({
                url: url,
                data: {
                    'customer': customer
                },
                success: function (data) {
                    $("#id_delivery_address").text(data);
                }
            });
        });


        {#col total calculation#}
        $(document).on("change",".quantity", function () {
            colum_calculation(".delivery_form", ".quantity", "#id_total_quantity")
        })


    </script>



    <script>
        $("#add-row").click(function () {
            var url = "{% url 'delivery note:model add row' %}"
            $.ajax({
                url : url,
                success : function (data) {
                    $(".delivery_form:last").after(data)
                    $(".django-select2").each(function () {
                        $(this).djangoSelect2()
                    })
                    last_formset_settings(".delivery_form", ".delivery_form:last", ".item", ".description", ".quantity")

                }

            })

        })

    </script>

{% endblock script %}


{% block end %}


    {{ deliver_main.media.js }}
    {{ delivery_transiction.media.js }}


{% endblock end %}

item select表单集为空,因此,当用户选择某个客户时,它将添加optio,具体取决于用户选择的客户,当在项目中添加用户select cutomer选项时,代码工作正常,但是当出现时,就会出现问题表单和模板中是否发生错误,显示错误消息,更改客户时使用ajax修改项目,选择反向返回空选项要求选择它

所以我想要一个解决方案,即使在错误消息出现在模板中的情况下,项目选择选项和用户选择的项目仍保留在项目选择中

0 个答案:

没有答案