#31330 closed Bug (fixed)
Trailing slash is missing in a "catchall" pattern in flatpages URLconf example.
Reported by: | Bryant Glisson | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | Documentation | Version: | 3.0 |
Severity: | Normal | Keywords: | append_slash, flatpage, catchall |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
We would like to append slashes to all URLs that do not have them, and we would also like to have a catchall flatpage expression, so our editors can enter whatever they like in that area of the CMS. The problem is that the flatpage validator adds the slash, but then only checks to see if a Flatpage exists for the resultant path (https://github.com/django/django/blob/master/django/contrib/flatpages/views.py#L41), whereas the is_valid_path method (https://github.com/django/django/blob/b9cf764be62e77b4777b3a75ec256f6209a57671/django/urls/base.py#L150) checks the urlconf, of which the catchall is a part, returns True, because the non-slash url fits the catchall. Hence, it does not redirect to the slashed version, making it impossible to reach and resulting in a 404.
Again, here is the flow:
- Attempt to reach "/my-valid-page-without-a-slash"
- is_valid_path returns True because "/my-valid-page-without-a-slash" fits the flatpage catchall and hence does not append a slash
- The page is not found in Flatpage entries, resulting in 404
It seems to me that if APPEND_SLASH is set to True, then we should not be looking in the urlconf for the page without the slash, but should instead immediately append the slash, then check the urlconf, in which case the correct pattern would be found and flatpages would never be called.
Change History (15)
comment:1 by , 5 years ago
Component: | Uncategorized → contrib.flatpages |
---|---|
Easy pickings: | set |
Type: | Uncategorized → Bug |
comment:2 by , 5 years ago
Summary: | Flatpage catchall is killing APPEND_SLASH functionality on normal pages → Flatpage catchall is killing APPEND_SLASH functionality on normal pages. |
---|
comment:3 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I would like to work on this ticket but could someone please guide me about how to solve this issue?
comment:4 by , 5 years ago
Ninad, there is nothing to work on for a moment. Ticket is still not accepted, we're waiting for confirmation from the reporter that it's a documentation regression.
comment:5 by , 5 years ago
Component: | contrib.flatpages → Documentation |
---|---|
Summary: | Flatpage catchall is killing APPEND_SLASH functionality on normal pages. → Trailing slash is missing in a "catchall" pattern in flatpages URLconf example. |
Triage Stage: | Unreviewed → Accepted |
meesterguyman, I understand that you're not using contrib.flatpages.forms.FlatpageForm
because in such case trailing slash is required when APPEND_SLASH
is True. I confirmed that when you allow for URLs without a trailing slash and want to use a “catchall” pattern then a trailing slash is required in:
path('<path:url>/', views.flatpage)
It's a regression in df41b5a05d4e00e80e73afe629072e37873e767a.
comment:7 by , 5 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:8 by , 5 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:12 by , 5 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
I think the addition of the slash causes a different problem.
Any valid page URL will match the catch all pattern, but the actual URL that is captured will not have the last slash in it. This will cause the flatpages view code to fail to find the page, add a slash to the URL and then return a permanent redirect to that URL with an extra slash on the end of it. So if the URL is '/someurl/', the user will be redirected to '/someurl//
'.
I think the last slash needs to be inside the captured part of the URL, which can be done with re_path, like this:-
re_path('^(?P<url>.+/)$', views.flatpage)
Thanks for this ticket, I think there is a regression introduced in df41b5a05d4e00e80e73afe629072e37873e767a in
flatpages
docs, i.e. a "catchall" pattern should contain a trailing slash when you useAPPEND_SLASH
:Can you check your app with this change?