最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Enable Django and Tastypie support for trailing slashes? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share edited May 13, 2012 at 22:36 egidra asked May 11, 2012 at 17:28 egidraegidra 9,08721 gold badges67 silver badges92 bronze badges 6
  • Have you looked at this module? github./PaulUithol/backbone-tastypie – thedjpetersen Commented May 11, 2012 at 17:39
  • related? stackoverflow./questions/4891879/… – fguillen Commented May 11, 2012 at 17:39
  • @thedjpetersen I am using Paul's backbone-tastypie right now. It works up until backbone-tastypie uses Backbone.sync because the default URL has a slash appended to the end. I have the opposite problem of the link that fguillen posted. I am only able to support URLs without slashes appended to the end. In the link above, it seems like they only support URLs with slashes at the end. – egidra Commented May 11, 2012 at 20:57
  • Sorry, I think I didn't understand: do you want slashes at the end of the URLs or not?. – fguillen Commented May 12, 2012 at 15:26
  • @fguillen I do not want slashes at the end of the URLs. When Backbone uses oldSync, it makes an HTTP request to a URL that has a slash at the end. Unfortunately, Django (or Tastypie) doesn't like when a URL has a slash at the end. I'll put more details in the body of the original post. – egidra Commented May 13, 2012 at 22:31
 |  Show 1 more ment

2 Answers 2

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论