如何在js文件中为PUT rest api request url添加斜线
How to add a slash to PUT rest api request url in js file
我正在使用 django restapi 和 backbone 制作一个待办事项应用程序。 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
。
chrome 留言:
RuntimeError at /api/lists/41
You called this URL via PUT, but the URL
doesn't end in a slash and you have APPEND_SLASH set. Django can't
redirect to the slash URL while maintaining PUT data. Change your form
to point to 127.0.0.1:8000/api/lists/41/ (note the trailing slash), or
set APPEND_SLASH=False in your Django settings.
Request Method: PUT Request URL: 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;
删除 ListsCollections
中的斜线
var ListsCollections = Backbone.Collection.extend({
model: List,
url : "http://127.0.0.1:8000/api/lists"
});
这是一个直接的解决方案。只需创建一个 Backbone 模型库 class,它会在查询特定对象时添加尾部斜线,并从中派生出您自己的所有模型。像这样:
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 一起使用,很久以前还与 tastypie 一起使用。很有魅力。
我正在使用 django restapi 和 backbone 制作一个待办事项应用程序。 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
。
chrome 留言:
RuntimeError at /api/lists/41
You called this URL via PUT, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining PUT data. Change your form to point to 127.0.0.1:8000/api/lists/41/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
Request Method: PUT Request URL: 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;
删除 ListsCollections
var ListsCollections = Backbone.Collection.extend({
model: List,
url : "http://127.0.0.1:8000/api/lists"
});
这是一个直接的解决方案。只需创建一个 Backbone 模型库 class,它会在查询特定对象时添加尾部斜线,并从中派生出您自己的所有模型。像这样:
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 一起使用,很久以前还与 tastypie 一起使用。很有魅力。