-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Option to have default router accept URLs without trailing slash #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seems reasonable, yeah. How would you feel about a simple Boolean? I'm not sure if an 'optional' case is really necessary/desirable. |
Boolean is totally fine. I solved my own problem temporarily. I just extended the SimpleRouter class
|
"I solved my own problem temporarily. I just extended the SimpleRouter class I will use this solution instead of the trailing_slash boolean (which, if false, insists that a trailing slash is NOT used), as I think it's needlessly frustrating to make user's worry about whether or not to use a trailing slash. What on Earth is the use case for causing a request to fail if it does/does not have a trailing slash? |
That is an excellent question. I believe it has something to do with some On Tue, Jul 30, 2013 at 9:12 PM, alexf101 notifications@github.com wrote:
|
I am subclassing |
As bobobo1618 said, literally copy and paste the list of routers, and replace everything there. It worked for me (I don't know why your solution didn't work for you). |
On the Angular side you can use a request interceptor to add the trailing slash back in as needed. |
If you want to accept both a trailing slash or no trailing slash, we have found this to work so far. (Though it has not been tested much yet.)
|
Excellent solution :) so simple
|
@alexf101 @benspaulding this solution will lead to duplication in the breadcrums in the browsable API. If this is ok for you, you can use it :) I like the changed regex in Default/SimpleRouter more. |
An update to what I said a few months back...
You will also need to override def get_lookup_regex(self, viewset):
"""
Given a viewset, return the portion of URL regex that is used
to match against a single instance.
"""
# Don't consume `.json` style suffixes
base_regex = '(?P<{lookup_field}>[^/.]+)'
lookup_field = getattr(viewset, 'lookup_field', 'pk')
return base_regex.format(lookup_field=lookup_field) This also does not cause issues within the browsable API. |
@syphar Thanks for pointing that out. We are not currently using the browsable API, so we didn’t notice it, but we are likely to in the future. Good to know. @kevin-brown Oh really? I’ll have to try again, then, because I could not get your solution to work. Thanks! |
FWIW, the fix for this issue has just landed in Angular: angular/angular.js@3878be5. The commit message contains info how to configure it. |
ember-data doesn't use trailing slashes by default: http://emberjs.com/guides/models/the-rest-adapter/#toc_url-conventions Is there a config option to allow the absence of trailing slashes yet in django-rest-framework? |
@kevin-brown I'm having trouble getting your solution to work. I've tried with and without overriding class MyAPIRouter(routers.SimpleRouter):
trailing_slash = "/?" |
Nevermind, I should have just read the documentation. http://www.django-rest-framework.org/api-guide/routers.html#simplerouter router = routers.SimpleRouter(trailing_slash=False) |
I've been testing with the |
Based on the comments here i ended up doing something like this: class OptionalSlashDefaultRouter(DefaultRouter):
def __init__(self, *args, **kwargs):
self.trailing_slash = '/?'
if 'root_renderers' in kwargs:
self.root_renderers = kwargs.pop('root_renderers')
else:
self.root_renderers = list(api_settings.DEFAULT_RENDERER_CLASSES)
super(SimpleRouter, self).__init__()
router = OptionalSlashDefaultRouter() |
@psychok7 does follow class cause any problems?
|
@adeelyounas i don't think i have tried that one, but i guess it shouldn't. Please let us know if it works properly. |
@psychok7 I did not find any issues so far but will share if I find one. |
Complete solution with Django 1.11
|
It'd just be nice to be able to create a default router which, when included in a URL configuration file, will accept URLs without a trailing slash.
This is entirely the fault of Angular.js which strips the slashes off its URLs and doesn't show any sign of changing. There are other developers who have had the same problem. The main issue is that Angular will POST without the slash and Django can't redirect that.
I know that it's possible to have this by building a router from scratch and using custom URLs for everything but it seems like a very bad way of doing things.
Ideally, I'd like to be able to initialize my router with something like
which would accept URLs of the form
Place/?$
instead ofPlace/$
The text was updated successfully, but these errors were encountered: