admin管理员组文章数量:1332873
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!
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!
Share Improve this question edited Nov 21, 2024 at 8:28 Maarten Ureel asked Nov 21, 2024 at 7:13 Maarten UreelMaarten Ureel 4756 silver badges18 bronze badges1 Answer
Reset to default 0Presumably 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)
本文标签: djangoUse existing page for error 404505 in WagailStack Overflow
版权声明:本文标题:django - Use existing page for error 404505 in Wagail - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742307197a2450155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论