Django与Ember.js

1 投票
1 回答
1239 浏览
提问于 2025-04-17 20:32

好的,我正在尝试用 Django 和 ember.js 连接起来,使用的是 django rest framework,还有一个叫做 ember-data-django-rest-adapter 的工具。我用来测试的应用是 ember.js 文档中提供的 todomvc 示例

现在,ember 应用可以成功请求服务器,并且得到了响应(200),但是页面上什么都没有加载出来。

这是来自 Django 服务器的响应:

[02/Mar/2014 13:51:09] "GET /todos/?_=1393789869335 HTTP/1.1" 200 177

这是通过 curl 获取的响应内容(注意:这个响应相当于请求 'localhost/todos/'):

$> curl -H 'Accept: application/json; indent=4' http:/localhost:8080/todos/?_=1393789869335
{
    "count": 2, 
    "next": null, 
    "previous": null, 
    "results": [
        {
            "title": "hmm why isnt this working now", 
            "isCompleted": false
        }, 
        {
            "title": "interesttinnggggg", 
            "isCompleted": false
        }
    ]
}

适配器的文档中说:“适配器假设每个 Django 模型有两个不同的端点。”第二个端点如下,它允许请求单个的 'todo':

$> curl -H 'Accept: application/json; indent=4' http:/localhost:8080/todos/1/
{
    "title": "hmm why isnt this working now", 
    "isCompleted": false
}

我觉得问题可能出在我的 application.js 文件里:

window.Todos = Ember.Application.create();

Todos.ApplicationAdapter = DS.DjangoRESTAdapter.extend({
  host: 'http://localhost:8080'
 });

 Todos.ApplicationSerializer = DS.DjangoRESTSerializer.extend();

如果有人有任何想法,任何帮助都将非常感激!如果需要,我可以发布更多代码。谢谢大家。

编辑:调试中。“请求的资源上没有 'Access-Control-Allow-Origin' 头。因此,来源 'null' 不被允许访问。” 另一个讨论让我觉得 CORS 可能能解决这个问题。我会再反馈进展。

1 个回答

1

在Django这边,不要使用 PaginationSerializer。因为 DjangoRESTAdapter 需要的是一个项目的数组,而不是一个描述页面的对象。来自ember-data-django-rest-adapter的 说明文档 中有相关的说明:

iii) 目前还不支持分页

撰写回答