1

我有一个模型表,我在 AJAX 操作上将其序列化为 JSON。

现在在客户端,我的模型结构为 JSON,但我很想在已发送的数据上使用 django 模板标签。

例如,假设我的 JSON 模型中有一个日期参数。django 序列化程序将对其进行序列化,我无法在客户端控制它,因为它已经被“编译”了。

有没有办法做这样的事情?

4

2 回答 2

1

If you want to format the data first, then send a rendered template fragment as your Ajax response, rather than JSON.

于 2012-07-18T17:25:05.380 回答
1

在客户端,您可以使用一些基于 javascript 的模板引擎,例如mustache

/* For example */
var json_data = {
    name: "Joe",
    amount: 10.55
};

var template = "{{name}} spends {{amount}}";
$('#some-div').html(Mustache.render(template, json_data));

在 django 模板上,如果没有这个 gist{{ stuff }}提供的非常方便的{% verbatim %}模板标签,就很难逃脱。

另一种方法是:使用静态文件作为客户端模板并通过 AJAX 调用获取它们。

于 2012-07-18T16:21:09.667 回答