在 Wagtail 多站点设置中为每个站点设置 404 [英] Having a 404 for each site in a Wagtail multisite setup

查看:38
本文介绍了在 Wagtail 多站点设置中为每个站点设置 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让两个不同的 Wagtail 站点拥有自己的 404 页面,但似乎没有办法在 wagtail设置"的站点"配置中指定将哪个页面用作 404 页面=>站点"部分,当我将它们放入所涉及的应用程序目录时,我似乎无法加载正确的 404:

I'm trying to get two different Wagtail sites to have their own 404 pages, but there does not appear to be a way to specify which page to use as 404 page in a "site" config in the wagtail "settings" => "sites" section, and I can't seem to get the correct 404 to be loaded when I put them into the app directories involved:

codebase/
  ./__init__.py
  ./manage.py
  ./apps/
     ./settings.py
     ./urls.py
     ...
     ./django-app-1/
     ./django-app-2/
     ./templates/
        ./404.html
     ./mainsite/
        ./migrations/
        ./static/
        ./templates/
           ./mainsite/
           ./404.html (this 404 always gets used)
     ./spinoff/
        ./migrations/
        ./static/
        ./templates/
           ./spinoff/
           ./404.html (this file never gets used)

所以在 INSTALLED_APPS 中我们有:

INSTALLED_APPS = [
  ...django apps...
  ...wagtail apps...

  'apps.mainsite',
  'apps.spinoff',
]

在这种情况下,主站点拥有大量所有页面类型,而在不同域上运行的衍生站点通过从 apps 导入它们使用这些页面类型.mainsite.

In this, the main site has the vast bulk of all the page types, and the spinoff site, which runs on a different domain, uses those page types by importing them from apps.mainsite.

在 Wagtail 中,我们有两个以 root 身份运行的页面:一个 Homepage 是一种 mainsite 页面类型,以及一个 Spinoff Homepage 是一个 spinof 页面类型,从主站点的页面类型继承.

In Wagtail we have two pages that work as root: a Homepage that is a mainsite Page type, and a Spinoff Homepage that is a spinof Page type that inherits from the mainsite's page type.

在站点设置中,我们有一个指向 mainsite.com 的站点条目,主 Homepage 设置为 Root,另一个站点条目指向 mainsite.comcode>spinoff.com,将衍生主页设为 root.

In the sites settings, we have one site entry that points to mainsite.com, with the main Homepage set as Root, and another site entry that points to spinoff.com, with the spinoff homepage set as root.

对于这两个站点,一个不存在的 url 请求会导致主站点的 404.html 被使用,所以问题是:我们如何使衍生域上不存在的 url 解析为衍生域的 404.html取而代之?

For both of these sites, an non-existent url request leads to the main site's 404.html getting used, so the question is: how do we make non-existent urls on the spinoff domain resolve to the spinoff's 404.html instead?

推荐答案

由于 Wagtail 基于 Django,您可以自定义错误视图.Wagtail的core.view.serve调用core.models.page.route,如果找不到页面路由,则引发Http404.因此,在 urls.py 中,您将输入:

Since Wagtail is built on Django, you can customize the error view. Wagtail's core.view.serve calls core.models.page.route, and if the page route isn't found, then it raises Http404. Therefore, in urls.py you would put:

从 yourapp.views 导入 custom404_view

handler404 = 'yourapp.views.custom404_view'

views.py 中:

from django.http import HttpResponseNotFound

def custom404_view(request, exception):       
    return HttpResponseNotFound('<h1>{}</h1>'.format(request.site))

我上面显示的内容返回 Wagtail 站点以说明该站点在视图中可用,因此在您的情况下,只需根据站点有条件地返回您的 HTML.

What I've shown above returns the Wagtail site to illustrate that the site is available in the view, so in your case, just return your HTML conditionally based upon the site.

这篇关于在 Wagtail 多站点设置中为每个站点设置 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆