Django发布请求无法正常运行

时间:2020-01-10 16:19:34

标签: jquery html django neo4j neomodel

我当前正在一个页面上工作,当用户单击一个表中的选项时,将更改该页面上其他表上填充的数据。发送请求,并通过它收到一条200消息,但页面不会重新加载。我什至在我的views.py中打印了所有变量,以确保我正在从数据库和POST请求中收集并更正数据。

我正在使用Neo4j数据库并使用neomodel进行查询。

我的html页面有一个csrf令牌,我的jquery可以正确看到它。

需要填充其中一个表的示例:

<div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                      <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Staff Needed: </div>

                      <h1>{{ staffNeeded }}</h1>

                    </div>
                    <div class="col-auto">
                      <i class="fas fa-dollar-sign fa-2x text-gray-300"></i>
                    </div>
                  </div>

我的views.py:

def staffinganalysis(request):
    contracts = ContractDataTable.getContractdata()

    allPositions = PositionsDataTable.getAllPositions()
    fillings = PositionsDataTable.getFillings()

    currentlyStaffed = len(fillings)

    if request.method == 'POST':
        postData = request.POST.copy()
        selectedContract = postData.get('contract')
        openings = PositionsDataTable.getOpenings(selectedContract)
        staffNeeded = len(openings)
        print(selectedContract)
        print(openings)

        return render(request,'staffingAnalysis.html',
                  {'contracts': contracts, 'openings': openings, 'allPositions': allPositions, 'fillings': fillings,
                   'staffNeeded': staffNeeded, 'currentlyStaffed': currentlyStaffed})

    if request.method == 'GET':
        return render(request, 'staffingAnalysis.html',{'contracts': contracts,'allPositions': allPositions})

我的javascript,用于处理数据表中元素的点击:

$(document).ready(function() {
  $('#contract_dataTable').DataTable();
  var CSRFtoken = $('input[name=csrfmiddlewaretoken]').val();

  var table = $('#contract_dataTable').DataTable();

  $('#contract_dataTable tbody').on('click', 'tr', function () {
        var contract = table.row( this ).data().toString();
       $.post('staffinganalysis',{
           contract: contract,
           csrfmiddlewaretoken: CSRFtoken,


       });

    } );
});

0 个答案:

没有答案