我有一个 js 代码,它在点击事件上发送一个 POST 请求到/productionFloor/wip/ 我有一个接收这些请求的 View (还将正确传递的所有参数打印到控制台)。 我正在尝试使用从 POST 请求接收到的数据来呈现模板,但 Django 不会呈现 View 。
这是我的看法:
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});
});
正如我提到的,服务器正确地收到请求并打印正确的数据,但它不会更改浏览器上的页面。
最佳答案
基本上,您使用的是来自 jQuery 的 ajax 调用。 Django 以 json/string 格式返回响应。在成功方法中,您必须进行更改以将其反射(reflect)在 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.
});
关于javascript - Django 在 POST 请求后不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580927/