从模板模型表单获取ID并显示db django ajax django模型表单中的数据

时间:2016-10-14 10:05:44

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

我有一个视图,首先从数据库中显示与该id相关的最新ID和数据。

请在此处查看示例表格。 http://i63.tinypic.com/s2gyhc.jpg

我想获取显示的ID,将其发送到我的视图,增加它并显示与下一个ID相对应的数据。

我已将ID发布到视图中。现在如何在不刷新的情况下显示新数据?

Ajax代码

$("#next").click(function(e){
    $.ajax({
          type:'POST',
          url:'/next/',
          data:{
                voucher_id:$('#id_voucher_id').val(),
                csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
          },
          success:function(){
                alert('yo!')
          }
    });
          return false;});

Views.py

def master_detail_next(request):

    author_form = TmpForm()

    ''' Decide what we want to show'''
    if request.method == 'POST':
        author = TmpForm(request.POST)
        voucher_id = str(int(request.POST['voucher_id']) + 1)

        ''' Define the three forms here'''
        author = TmpPlInvoice.objects.get(voucher_id=voucher_id)
        author_form = TmpForm(instance=author)

        return render_to_response('main.html',
                              {'form': author_form},
                             context_instance=RequestContext(request))

我如何在Django模型表单/ Ajax中执行此操作?

1 个答案:

答案 0 :(得分:0)

找到正确的方法。 需要更新的表单应该在另一个模板文件中。

    $("#previous").click(function() {
    $.ajax({
          type:'POST',
            url:'/next/',
            data:{
                voucher_id:$('#id_voucher_id').val(),
          csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
            },
            success:searchSuccess,
            dataType: 'html'
        });
   });


        function searchSuccess(data, textStatus, jqXHR)
          {
            $('#myForm').html(data);
       }