0

我正在用 django restapi 和主干制作一个待办事项应用程序。c、r、d 已完成,但是当我尝试更新时,PUT 请求没有斜杠http: //127.0.0.1:8000/api/lists/41而不是http: //127.0.0.1:8000/api/lists/41/. 我得到一个500 internal server error.

铬消息:

/api/lists/41 处的运行时错误

您通过 PUT 调用了此 URL,但该 URL 不以斜杠结尾,并且您设置了 APPEND_SLASH。Django 在维护 PUT 数据时无法重定向到斜杠 URL。将表单更改为指向 127.0.0.1:8000/api/lists/41/(注意尾部斜杠),或在 Django 设置中设置 APPEND_SLASH=False。

请求方法:PUT 请求地址: http: //127.0.0.1 :8000/api/lists/41

根据我添加的消息APPEND_SLASH = False,所有restapi响应都失败了。

我的 scripts.js 文件:

/**
 * Created by Manoj on 6/29/2016.
 */


var List = Backbone.Model.extend({
    defaults:
    {
        "work": "",
        "done": false
    }
});

var ListsCollections = Backbone.Collection.extend({
      model: List,
      url : "http://127.0.0.1:8000/api/lists/"
});


var ListView = Backbone.View.extend
({
    tagName : "tr",
    listtemplate: _.template($('#list2-template').html()),

    render: function() {
      this.$el.html(this.listtemplate(this.model.attributes));
      //this.$el.html("afsfa");
      return this;
    }
});

var ListsView = Backbone.View.extend({
    el: "#table-body",
    model : ListsCollections,

    // events:{
    //     'click #add': 'addList'
    // },

    initialize : function(){
        $("#table-body").html('');
        this.render();
    },

    render:function(){
        var c = new ListsCollections,i=1;
        self = this;
        c.fetch({
            success : function(){
            self.$el.html('');
                c.each(function(model){
                    var stud_ = new ListView({
                        model : model,
                    });

                    self.$el.append(stud_.render().el);
                });
            }
        });

        //Rendering on to the screen
        return this;
    },

    addList: function (e) {
        e.preventDefault();
        var temp = new Backbone.Collection;
        $("#details").html('<input type="text" id="work_input"/><input type="checkbox" id="done_input"/><input id="clicker" type="submit"/>');
        $("#clicker").click(function(){

            var temp1 = new ListsCollections;
            temp1.create({
                userid: 1,
                work : $("#work_input").val(),
                done : $("#done_input").val()
            });
            $("#details").html('');
            var k = new ListsView;
            k.render();
            parent.location.hash='';
        });
    }
});


//Creating route paths
var myRouter = Backbone.Router.extend({

    routes : {
        "lists/add" : "addList",
        "lists/delete/:id" : "deleteList",
        "lists/update/:id" : "updateList"
    },

    addList : function()
    {
        $("#details").html('<input type="text" id="work_input"/><input type="checkbox" value = "TRUE" id="done_input"/><input id="clicker" type="submit"/>');
        var user = user;
        $("#clicker").click(function(){

            var temp1 = new ListsCollections;
            temp1.create({
                userid: 1,
                work : $("#work_input").val(),
                done  : document.getElementById('done_input').checked
            });
            $("#details").html('');
            var k = new ListsView;
            k.render();
            parent.location.hash='';
        });

    },

    deleteList : function(e){
        var temp = new ListsCollections;
        temp.fetch({
            success : function(){
                temp.findWhere({id : parseInt(e)}).destroy({
                    'success': function () {
                        var k = new ListsView;
                        k.render();
                        parent.location.hash='';
                    }
                });
            }
        })
    },

    updateList : function(eid){
        $("#details").html('<input type="text" id="work_input" value=""/><input type="checkbox" id="done_input"/><input id="clicker" type="submit"/>');
            $("#clicker").click(function(){
                var temp1 = new ListsCollections;
                temp1.fetch({
                    'success' : function()
                    {
                        var tag = temp1.get(parseInt(eid));
                        tag.set({"work" : $("#work_input").val()});
                        tag.set({"done"  : document.getElementById('done_input').checked});
                        tag.save(null,
                            {
                                "success" : function () {
                                $("#details").html('');
                                var k = new ListsView;
                                k.render();
                                parent.location.hash='';
                            }}
                        );

                    }
                })
            });
    },

    updateList2: function (e) {
        $("#details").html('<input type="text" id="work_input" value=""/><input type="checkbox" id="done_input"/><input id="clicker" type="submit"/>');
        $("#clicker").click(function () {

        })
    },
});

var router = new myRouter();
Backbone.history.start();
var app = new ListsView;
4

2 回答 2

1

这是一个直截了当的解决方案。只需创建一个 Backbone Model 基类,该基类在查询特定对象时添加尾部斜杠,并从中派生所有您自己的模型。像这样:

var DjangoModel = Backbone.Model.extend({
    // if backbone wants a specific object, append the slash
    url : function() {
        if (this.get('id')) {
            return this.collection.url + this.get('id') + '/';
        }
        else {
            return this.collection.url;
        }
    }
});

我在默认配置中将此解决方案与 Django Rest Framework 一起使用,而在更早之前,我也将它与 sweetpie 一起使用。奇迹般有效。

于 2016-07-02T22:42:40.767 回答
0

删除斜线ListsCollections

var ListsCollections = Backbone.Collection.extend({
  model: List,
  url : "http://127.0.0.1:8000/api/lists"
});
于 2016-07-02T18:19:16.080 回答