My problem is that Backbone is trying to do an HTTP request on a URL with a slash at the end, like the following:
:8000/api/v1/update/2/
For some reason, Django (or tastypie) is not accepting URLs with slashes at the end, so the above URL will not work, but this URL does work:
:8000/api/v1/update/2
Backbone-tastypie falls back to oldSync, which is the original sync that comes with Backbone, when not using its overridden sync. I believe that when Backbone-tastypie uses oldSync, it appends a slash at the end of the URLs, which I do not want.
Most of the solutions being suggested are dealing with the opposite problem I have. They are trying to fix the problem where trailing slashes work, but no trailing slashes do not work.
I need to be able to support trailing slashes in addition to non-trailing slashes. This can be fixed in two ways:
How do I change the backbone-tastypie code so that no AJAX calls append the slash at the end?
How do I make it so that Django/tastypie will treat the above two URLs as the same?
Either one of those will fix my problem, but I cannot figure out how to implement any of them.
You can tell Tastypie/Django to allow or disallow trailing slashes.
Look here
For a Backbone
solution:
You can overwrite the default behavior of Model.url
, even using the normal one and making a small modification like the one you are looking for:
// code simplified and not tested
var MyModel: Backbone.Model.extend({
url: function() {
var original_url = Backbone.Model.prototype.url.call( this );
var parsed_url = original_url + ( original_url.charAt( original_url.length - 1 ) == '/' ? '' : '/' );
return parsed_url;
}
});
Same aplies for Collection
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With