链接到 Wagtail CMS 中的特定页面 [英] Link to specific page in Wagtail CMS

查看:28
本文介绍了链接到 Wagtail CMS 中的特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Wagtail CMS 中,我不知道如何构建指向特定页面的链接.我想要一个(固定的,非创作的)模板中的链接,从我的 BlogIndexPage 到我的 BlogIndexArchivePage,反之亦然.在官方文档 pageurl 看起来很有希望:

In Wagtail CMS I don't know how to construct a link to a specific page. I want a (fixed, not authored) link in the templates from my BlogIndexPage to my BlogIndexArchivePage and vice versa. In the official docs pageurl looks promising:

{% load wagtailcore_tags %}
...
<a href="{% pageurl self.blog_page %}">

但我不理解符号blog_page,但page-classes 的名称类似于BlogPageBlogIndexPage.我是否必须在 models.py 中定义要链接到的页面 - 如果是这样:如何?

but I don't understand the notation blog_page, but the page-classes are named like BlogPage or BlogIndexPage. Did I have to define the page I would link to in my models.py - and if so: how?

推荐答案

在我给出详细答案之前,我会提出一个简单的建议:假设您已经为您的博客索引和博客存档页面提供了诸如 /blog//blog/archive/,是否有任何理由相信它们永远会改变?如果没有,那么只需在您的模板上编写一个硬编码的 <a href="/blog/"> 链接并忘记它.这似乎是一种逃避,但您将需要某种持久的方式来指定特定页面作为博客索引 - 无论是基于页面类型、ID 还是在您的位置站点结构 - 并且通过 URL 执行此操作可能比您可以提出的任何其他机制更明智且面向未来.URL 应该是持久标识符,毕竟...

Before I give the detailed answer, I'll make a simple suggestion: assuming you've already given your blog index and blog archive pages sensible URLs like /blog/ and /blog/archive/, is there any reason to believe that they're ever going to change? If not, then just write a hard-coded <a href="/blog/"> link on your template and forget about it. That might seem like a cop-out, but you're going to need some persistent way of nominating a particular page as the blog index - whether that's based on page type, or ID, or location within your site structure - and doing that by URL is probably more sensible and future-proof than any other mechanism you could come up with. URLs are supposed to be persistent identifiers, after all...

好的.详细回答:

pageurl 文档的示例中,self.blog_page 只是一个引用 Page 对象的变量.如果你有这样的设置,你会使用它,其中 blog_page 是你页面的一个字段,指向其他页面:

In the example for the pageurl documentation, self.blog_page is simply a variable that refers to a Page object. It's what you'd use if you had a setup like this, where blog_page is a field of your page, pointing to some other page:

class ExamplePage(Page):
    body = RichTextField()
    blog_page = models.ForeignKey('wagtailcore.Page')

    content_panels = Page.content_panels + [
        FieldPanel('body'),
        PageChooserPanel('blog_page'),
    ]

但是,在您的情况下,您希望以编程方式确定链接页面,而不是创作页面内容的一部分,因此您需要一些其他方式来获取相关的 Page 对象.

However, in your case, you want the linked page to be determined programmatically, not part of the authored page content, so you need some other way of obtaining the relevant Page object.

当您谈论我的 BlogIndexPage"时,您是在暗示该类型的页面在任何时候都将存在于您的网站上.如果这确实是一个有效的假设,那么用于检索该页面的合适 Django 表达式将是:BlogIndexPage.objects.first().(如果这不是一个有效的假设,那么您需要自己决定您的站点将如何选择一个特定的 BlogIndexPage 作为 博客索引.)

When you talk about "my BlogIndexPage", you're implying that exactly one page of that type will exist on your site at any one time. If that is indeed a valid assumption, then a suitable Django expression for retrieving that page would be: BlogIndexPage.objects.first(). (If that's not a valid assumption, then you'll need to decide for yourself how your site is going to pick one particular BlogIndexPage as the blog index.)

您现在需要使该页面对象在您的模板上可用.一种方法是通过 get_context 方法:

You now need to make that page object available on your template. One way of doing this is through a get_context method:

class ExamplePage(Page):
    body = RichTextField()

    def get_context(self, request):
        context = super(ExamplePage, self).get_context(request)
        context['blog_index'] = BlogIndexPage.objects.first()
        return context

这使它在模板上作为变量 blog_index 可用,允许您编写:{% pageurl blog_index %}.

This makes it available on the template as the variable blog_index, allowing you to write: {% pageurl blog_index %}.

这篇关于链接到 Wagtail CMS 中的特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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