django - Use existing page for error 404505 in Wagail - Stack Overflow

I am using the most recent version of Wagtail, 6.3.1 at this time.I want to use a specific page for an

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742307197a4419177.html

相关推荐

  • django - Use existing page for error 404505 in Wagail - Stack Overflow

    I am using the most recent version of Wagtail, 6.3.1 at this time.I want to use a specific page for an

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信