Opened 5 years ago

Closed 5 years ago

#30895 closed Bug (invalid)

Multiples trailing slashes appended to URL

Reported by: Conor Cunningham Owned by: nobody
Component: Core (URLs) Version: 2.2
Severity: Normal Keywords: urls, url
Cc: Conor Cunningham Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Conor Cunningham)

I'm not sure whether this is a feature or a bug, but failing in my attempt to get an answer in #django or django-users, I thought it best to report it.

Summary

In short I have a URL which will be either /api/v1/case/ if APPEND_SLASH=True, or /api/v1 case if APPEND_SLASH = False.
The URL /api/v1/case/ is made up of two URLS, namely /api/v1 and /case, one from the projects urls.py and the other from the app's urls.py. What I suspect is happening is that a trailing slash is being appended to both the URLS before they are concatenated to make the final URL.

Django Version: 2.2.6
Installed Apps:
django-allauth==0.40.0
django-cors-headers==3.1.1
django-crispy-forms==1.7.2
django-environ==0.4.5
django-rest-auth==0.9.5
django-rest-swagger==2.2.0
djangorestframework==3.10.3
djangorestframework-simplejwt==4.3.0

Reproduction

It should be first mentioned that I am using DRF to provide API functionality for my views mentioned herein, and that I am using Docker both locally for development, and on the production server. The issue is reproducible in both environments.

I should also note that I can avoid this behaviour by configuring the URLs in a different (what I presume to be the correct way)

To reproduce this, I have a project urls.py file with the following

path('api/v1', include('api.urls')), - (note there is no trailing slash)

And an app called api with a URL file, api/urls.py with the following

path('case', CasesView.as_view(), name='all-cases'),

If you have a URL configured as above and APPEND_SLASH=True you will be able to navigate to the URL /api/v1/case/. If APPEND_SLASH=False, then navigating to the aforementioned URL, you will receive a 404 and Django debug will report that it tried a URL of /api/v1 case

In addition to what appears to be two slashes being rendered into the final URL, I am also curious as to where the blank space in the URL /api/v1 case comes from, as there is no whitespace in either of the two configured URLs.

I can avoid this behaviour entirely, regardless of what APPEND_SLASH is set, by configuring the project URl as:
path('api/v1/', include('api.urls')),

and the app URL as:

path('case', CasesView.as_view(), name='all-cases'),

The difference only being in that I have hardcoded a trailing slash into the project URL's path.

Final Thoughts

The reason I think this is a bug, is that the behaviour, at least to me is non-obvious. That's not to say when you stop and think about it that it isn't perhaps logical, if indeed there are two URLs which have slashes appended to them in order to make the final URL, but on first glance when I read about Django's APPEND_SLASH feature, I think of the final URL, the one that the user will see, not of two separate URLs that are being put together to make the final product.

However, the thing that makes me think that perhaps this isn't a bug, is that I seemingly just didn't configure Django properly. It could be as simple as that, and I apologise if that is the case.

I am only too happy to provide any further information that may be of use in investigating this issue, if indeed it turns out to be an issue.

Cheers,

Conor

Attachments (1)

djang-urls.png (240.1 KB ) - added by Conor Cunningham 5 years ago.
Screenshot showing a whitespace in URLS that would otherwise be filled by a slash if APPEND_SLASH=True

Download all attachments as: .zip

Change History (4)

comment:1 by Conor Cunningham, 5 years ago

Description: modified (diff)

by Conor Cunningham, 5 years ago

Attachment: djang-urls.png added

Screenshot showing a whitespace in URLS that would otherwise be filled by a slash if APPEND_SLASH=True

comment:2 by Conor Cunningham, 5 years ago

Cc: Conor Cunningham added

comment:3 by Carlton Gibson, 5 years ago

Resolution: invalid
Status: newclosed

Hi Conor.

I think this is a mis-understanding on your part here. Alas, it's not something we can address directly on the issue tracker. See TicketClosingReasons/UseSupportChannels, which I know is where you began…

APPEND_SLASH does not affect the available URLs. Rather it tries an additional request to the same URL (if it's missing a slash) to see if that matches. Docs for APPEND_SLASH.

When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended.

So I'd try api/v1/case, get a 404 and be redirected to api/v1/case/ to give that a chance to match.

See the docs for Including other URLconfs to get your configuration correct. Note the examples look like this:

from django.urls import include, path

urlpatterns = [
    # ... snip ...
    path('community/', include('aggregator.urls')),
    path('contact/', include('contact.urls')),
    # ... snip ...
]

i.e. they do include the trailing slash when using include().

(From your screenshot, I'd guess api/v1case would match, but that's not what you're intending...)

Good luck.

Note: See TracTickets for help on using tickets.
Back to Top