django render 返回数据及使用
1. 前端页面标签中使用
(a) 后端返回代码
return render(reqeust, "template.html", {"status": 200});
(b) 前端使用代码 template.html
<html>
<div>
<p>后端返回的状态是:{{ status }}</p>
</div>
</html>
2. 前端页面js代码中使用
(a) 后端返回代码
# 后端代码一定要用json.dumps进行序列化
return render(reqeust, "template.html", {"status": json.dumps(200)});
(b) 前端使用代码 template.html
<html>
<div>
<script type="text/javascript">
var status={{ status|safe }}
</script>
</div>
</html>
- 直接返回HttpResponse 中指明json
# 后端代码不用render
return HttpResponse(json.dumps(value),content_type="application/json");
2134

被折叠的 条评论
为什么被折叠?



