Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Django and Tastypie support for trailing slashes?

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:

  1. How do I change the backbone-tastypie code so that no AJAX calls append the slash at the end?

  2. 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.

like image 239
egidra Avatar asked Dec 16 '22 00:12

egidra


2 Answers

You can tell Tastypie/Django to allow or disallow trailing slashes.

Look here

like image 81
enticedwanderer Avatar answered Feb 02 '23 02:02

enticedwanderer


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.

like image 39
fguillen Avatar answered Feb 02 '23 01:02

fguillen