POST请求后django不呈现

时间:2018-05-29 09:30:31

标签: javascript django rendering

我有一个js代码,它在on click事件上向/ 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});
    });

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

1 个答案:

答案 0 :(得分:3)

基本上你正在使用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.
    });
相关问题