I am trying to set up django flat pages that would be accessible via a url like /pages/page1.html instead of /pages/page1.
Followed the flatpages docs, and instead of the flatpages middleware used this code in the root urls.py (in the same folder with settings.py):
re_path('pages/.*\.html$', include('django.contrib.flatpages.urls')),
But this results in a 404 error.
I tried specifying the extension as a non-capturing group:
re_path('pages/.*(?:\.html)$', include('django.contrib.flatpages.urls')),
but I'm still getting a 404.
What would be the correct way to have the .html suffix for flatpages?
You can't use
includethat way. The.*part of the match will simply be thrown away, and not passed to the flatpage view.But you can hook up your flatpages view directly, and capture the url part in a group.
Since the flatpages app requires the
urlpart to have both a leading and trailing slash (/foobar/), you must adapt theflatpageview function with a decorator that replaces the.htmlextension with a/.Alternatively, you could simply write your own flatpage view. It doesn't do anything very complicated.
There are more examples of how to configure flatpages routes in the docs. https://docs.djangoproject.com/en/2.0/ref/contrib/flatpages/#using-the-urlconf