Django flatpages catch all

112 Views Asked by At

I am using the catch all pattern for flatpages in Django, like this:-

urlpatterns = [
    path('somepath/', include('project.somepathapp.urls')),
    path('anotherpath/', include('project.anotherpathapp.urls')),
    etc.
]

urlpatterns += [
    path('<path:url>/', views.flatpage),
]

In my template, I use:-

<a href="/about-us/">About Us</a>

to get to the About flatpage. But the URL pattern strips off the final slash and passes the flatpage view the URL /about-us (as opposed to /about-us/). The flatpage view then spots that the URL doesn't end in a slash, adds it back in, finds the page and redirects to that URL but adds an extra slash so that the URL is now /about-us//

If I remove the final slash from the catch all pattern, any URL from the main paths (somepath/ and anotherpath/) without the final slash is matched by the catch all pattern before the APPEND_SLASH is used and, since there is no page with that URL, the user gets a 404. So a URL like /somepath/ will work but /somepath won't.

What have I done wrong? It seems like a catch-22 to me. I can't use the middleware option because that doesn't always get passed through other middleware views so I'm stuck.

Any ideas?

0

There are 0 best solutions below