I am using the most recent version of Wagtail, 6.3.1 at this time.
I want to use a specific page for an error 404 and error 500 so I can have it in line of the rest of website.
I created two views:
class Error404View(View):
def get(self, request, *args, **kwargs):
site_settings = SiteSettings.for_request(request)
if site_settings.error_404_page:
serve = site_settings.error_404_page.serve(request, *args, **kwargs)
return serve
else:
return HttpResponseNotFound("NOT FOUND")
class Error500View(View):
def get(self, request, *args, **kwargs):
site_settings = SiteSettings.for_request(request)
if site_settings.error_500_page:
return site_settings.error_500_page.serve(request, *args, **kwargs)
else:
return HttpResponseServerError("INTERNAL SERVER ERROR")
(I know I will also to override the HTTP return code in the serving).
And in urls.py configured these:
handler404 = Error404View.as_view()
handler500 = Error500View.as_view()
if settings.DEBUG:
# Error test pages
urlpatterns += [path("404/", Error404View.as_view())]
urlpatterns += [path("500/", Error500View.as_view())]
But the output of those pages is simply:
<!DOCTYPE HTML>
<html lang="nl" dir="ltr">
<head>
<title>Office365</title>
</head>
<body>
<h1>Office365</h1>
</body>
</html>
(I selected the content page titled Office365 for the sake of testing).
This seems to be because the template of the returned Page object is the base template and not the page specific one:
I found this related question on SO but it is from many versions ago so perhaps it does not apply anymore: Wagtail: render a page programmatically in a Class Based View
So; how can I get the content of that specified page in the code of the view?
Thanks!
Presumably your site_settings.error_404_page
/ site_settings.error_500_page
field is a ForeignKey to the Page model, so that will give you an instance of the basic Page class (which just contains the fields common to all page types, such as title), rather than the specific class where all your page content fields are defined. To obtain the full object, use page.specific
:
serve = site_settings.error_404_page.specific.serve(request, *args, **kwargs)