在 Wagtail 多站点设置中为每个站点设置 404
Having a 404 for each site in a Wagtail multisite setup
我试图让两个不同的 Wagtail 网站拥有它们自己的 404 页面,但似乎没有办法在 "site" 配置中指定哪个页面用作 404 页面wagtail "settings" => "sites" 部分,当我将它们放入所涉及的应用程序目录时,我似乎无法加载正确的 404:
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
.
在 Wagtail 中,我们有两个以根用户身份工作的页面:Homepage
是 mainsite
页面类型,Spinoff Homepage
是 spinof
页面类型继承自主站点的页面类型。
在站点设置中,我们有一个指向 mainsite.com
的站点条目,主 Homepage
设置为 Root,另一个站点条目指向 spinoff.com
,其中衍生主页设置为 root。
对于这两个站点,一个不存在的 url 请求导致主站点的 404.html 被使用,所以问题是:我们如何使不存在的 url ]s 在衍生域上改为解析为衍生域的 404.html?
由于 Wagtail 是基于 Django 构建的,因此您可以 customize the error view。 Wagtail 的 core.view.serve
调用 core.models.page.route
,如果未找到页面路由,则会引发 Http404
。因此,在 urls.py
中,您将输入:
from yourapp.views import 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))
我在上面显示的 returns Wagtail 站点说明该站点在视图中可用,因此在您的情况下,只需 return 您的 HTML 有条件地在网站上。
我试图让两个不同的 Wagtail 网站拥有它们自己的 404 页面,但似乎没有办法在 "site" 配置中指定哪个页面用作 404 页面wagtail "settings" => "sites" 部分,当我将它们放入所涉及的应用程序目录时,我似乎无法加载正确的 404:
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
.
在 Wagtail 中,我们有两个以根用户身份工作的页面:Homepage
是 mainsite
页面类型,Spinoff Homepage
是 spinof
页面类型继承自主站点的页面类型。
在站点设置中,我们有一个指向 mainsite.com
的站点条目,主 Homepage
设置为 Root,另一个站点条目指向 spinoff.com
,其中衍生主页设置为 root。
对于这两个站点,一个不存在的 url 请求导致主站点的 404.html 被使用,所以问题是:我们如何使不存在的 url ]s 在衍生域上改为解析为衍生域的 404.html?
由于 Wagtail 是基于 Django 构建的,因此您可以 customize the error view。 Wagtail 的 core.view.serve
调用 core.models.page.route
,如果未找到页面路由,则会引发 Http404
。因此,在 urls.py
中,您将输入:
from yourapp.views import 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))
我在上面显示的 returns Wagtail 站点说明该站点在视图中可用,因此在您的情况下,只需 return 您的 HTML 有条件地在网站上。