如何从主页(页面)重定向到您的自定义应用程序模型

how to redirect from HomePage(Page) to your custom app model

我创建了一个应用程序 "Blog"。在我的应用程序中,我有几个模型,包括 "BlogIndex(Page)"。当我 运行 本地服务器时,我发现自己在 "home_page.html"。我想要的是在 "blog_index.html" 启动我的本地服务器。我知道我可以在 settings>site>localhost 设置一个根页面来使我的 "blog_index.html" 成为一个根页面,但我不能这样做,因为在我的应用程序中我有一些其他的模型同时存在级别为 "BlogIndex(Page)",它们是根 "HomePage" 的子级,因此它会破坏我的代码。所以我的问题是:我可以从 "HomePage(Page)" 重定向到我的 "BlogIndex" 以便在我启动服务器时自动从 "HomePage" 重定向到 "BlogIndex" 吗?我该怎么做?它会对网站的性能及其优化产生多大的影响?

我知道有设置>重定向,但它只适用于非活动页面,但我需要 "HomePage" 才能激活。 谢谢你。

也许更好的方法是在主页上显示您的博文(以及您想要的任何其他模型)。只需覆盖 get_context()。看这里:

更新: 您可以通过覆盖 serve() 方法来重定向。例如,在你的模型中,你会做类似的事情:

# home/models.py
...
from django.http import HttpResponseRedirect
from django.urls import reverse

class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]

    def serve(self, request):
        # Redirect to blog index page
        return HttpResponseRedirect('/blog/')

        # only do this if you're using urls.py and namespaces
        # return HttpResponseRedirect(reverse('blog:index'))

更多信息:http://docs.wagtail.io/en/latest/reference/pages/model_recipes.html?highlight=serve()#overriding-the-serve-method