【问题标题】:django does not render after POST requestdjango 在 POST 请求后不呈现
【发布时间】:2018-05-29 09:30:31
【问题描述】:

我有一个 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});
    });

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

【问题讨论】:

  • 您正在发出 ajax 请求,因此 $.post 将具有一个回调函数,该函数将返回您呈现的视图。您需要将该视图添加到您的 html 页面
  • 在发布请求后在您的 js 代码中添加 console.log()。

标签: javascript django rendering


【解决方案1】:

基本上,您正在使用来自 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.
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2017-12-09
    • 2022-01-26
    • 2019-09-09
    • 1970-01-01
    • 2020-08-31
    • 2017-05-02
    相关资源
    最近更新 更多