3

我有一个 js 代码,它在点击事件上向 /productionFloor/wip/ 发送一个 POST 请求我有一个接收这些请求的视图(也将所有正确传递的参数打印到控制台)。我正在尝试使用从 POST 请求接收到的数据呈现模板,但 django 不会呈现视图。

这是我的看法:

def wip_view(request):
if request.method == "POST":
    print(request.POST['id'])
    print(request.POST['process'])
return render(request, 'wip.html', {'nbar': 'production_floor'})

这是js:

        $(".clickable-schedule-row").dblclick(function() {
        id=$(this).children('td')[6].innerHTML;
        process=$(this).children('td')[7].innerHTML;
                    $.post("/productionFloor/wip/", {'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val(),
                            'id': id, 'process': process});
    });

正如我所提到的,服务器正确获取请求并打印正确的数据,但它不会更改浏览器上的页面。

4

1 回答 1

4

基本上你正在使用来自 jQuery 的 ajax 调用。Django 以 json/string 格式返回响应。在成功方法中,您必须进行更改以将其反映在 HTML 页面中。

 $.ajax({
      type: "GET",
      url: '/productionFloor/wip/',
      data:"{'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val(),
                        'id': id, 'process': process}",
      success: function(response) {
         // inside this function you have to bind html content using javascript, because ajax call will not render complete page.
    });
于 2018-05-29T09:37:46.540 回答