Django,模态形式的编辑按钮不起作用

时间:2020-07-10 08:27:05

标签: javascript jquery django django-forms django-views

在我的Django应用程序之一中,我设置了以下架构:

#models.py
    class Income(models.Model):
        price = models.DecimalField()
        quantity = models.DecimalField()
        date=models.DateField()

# forms.py
    class IncomeForm(forms.ModelForm):
        class Meta:
            model = Income
            fields = "__all__"

#views.py
def income_(request):
    elements = Income.objects.all()
    if request.method == 'POST':
         form = IncomeForm(request.POST)
         if form.is_valid():
             new_input = form.save()
    else :
       form = IncomeForm()
       elements = Income.objects.all()

    context= {
       'form': form,
       'elements':elements,
            }
    return render(request, "income/income.html", context)

def edit_income(request):
    pk = request.GET.get('pk')
    object = get_object_or_404(Income, pk = pk)
    form = IncomeForm(instance=object)
    return render(request, 'edit_income.html', {
        'object': object,
        'pk': pk,
        'form': form,
        })

之后,我设置了以下urlspatterns:

path('magazzino/gestionemagazzino/', views.income_, name ='gestione_magazzino'),
path('magazzino/edit_modal/<int:materiale_pk/>', views.edit_income, name='edit_income')

在我的Income.html文件中,我设置了以下内容

{% load crispy_forms_tags %}
 <form id="" method="post">                    
   <div class="form-group col-2 0 mb-0" >
       {{form.quantity|as_crispy_field}}
   </div>
   <div class="form-group col-2 0 mb-0" >
       {{form.price|as_crispy_field}}
   </div>
   <div class="form-group col-2 0 mb-0" >
      {{form.date|as_crispy_field}}
   </div>
</div>

此后,我创建了一个列出所有填充数据的表,并在最后一列中设置了以下按钮:

<button id="myBtn" data-target="#myModal"> </button> 

现在,我想为每一行创建一个按钮,以打开模式窗体,这使我可以修改每个id数据集的特定数据。

我尝试执行以下脚本:

$(document).ready(function(){
  $("#myBtn").click(function(){
    var pk = $(this).data('pid')
    $("#myModal").modal("show");
  });
  $("#myModal").on('show.bs.modal', function(event){
    var modal = $(this)
    var pk = $(this).data('pid')
    $.ajax({
        data: {'pk': pk},
        url: "{% url 'edit_income' %}",
        context: document.body,
        error: function(response, error) {
            alert(error);
        }
    }).done(function(response) {
        modal.html(response);
    });
  });
});

这将打开以下edit_income.html:

<div class="modal-dialog modal-lg" role="document">
    <form action="{% url 'edit_paper' pk=object.pk %}" method="post" class="form" enctype="multipart/form-data">
      {% csrf_token %}
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title">Edit Income</h4>
            </div>
            <div class="modal-body">
              {% csrf_token %}
              {{form|crispy}}
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <input type="submit" class="btn btn-primary" value="Save changes" />
            </div>
        </div><!-- /.modal-content -->
    </form>
</div>

但是,如果我按该按钮,则无济于事。问题出在哪里?

0 个答案:

没有答案
相关问题