如何在主页内使用博客索引?

问题描述 投票:0回答:2

我是wagtail的新手,也是django的新手。我想知道如何实现这里文档中的博客。

https:/docs.wagtail.ioenstablegetting_startedtutorial.html。

但直接在主页内。意思是,我希望博客索引是网站的根基(就像大多数博客网站一样)。

先谢谢你了

django wagtail
2个回答
0
投票

很简单,在urls.py中使用重定向,就像下面的代码。

from django.views.generic import RedirectView
urlpatterns = [
    path(r'^$', RedirectView.as_view(url='/blog/', permanent=False)),
    # pass other paths
]

0
投票

你可以直接添加你的博客 "帖子"(如BlogPage)作为首页下的直接子女。

这将意味着你的博客页面的URLs将直接在根URL下,例如。mydomain.com/my-cool-post/.

注意:主页下的其他页面也会共享这个路径区域(如:.com)。/contact-us/).

基本上只需按照教程中的步骤进行操作,但忽略了关于以下部分的内容 BlogIndex. 保持你的 BlogPage 模型,并在管理界面中添加子女时,将其添加到主页下。

如果你想列出所有的文章,你的 HomePage 模板,你可以修改模板上下文来返回 blog_pages 类似于 文件.

你可以通过以下方式过滤一个页面查询集 类型使用 exact_type. 或者,如下图所示,你可以使用 BlogPage.childOf(...) 的方式进行查询。

关于Django的文档 查询集api.

my-app/models.py
class HomePage(Page):
    body = RichTextField(blank=True)

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

    def get_context(self, request):
        context = super().get_context(request)

        # Add extra variables and return the updated context
        # note: be sure to get the `live` pages so you do not show draft pages
        context['blog_pages'] = BlogPage.objects.child_of(self).live()
        return context

my-app/templates/home_page.html
{{ page.title }}

{% for blog_page in blog_pages %}
    {{ blog_page.title }}
{% endfor %}
© www.soinside.com 2019 - 2024. All rights reserved.