InlinePanel中参考对象的图像渲染

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

在我的文章pageModel中,我有一个InlinePanel('technologies', label="Technologies"),,它加载了一个正在使用ArticlesPageTechnologies(Orderable)PageChooserPanel('technologies', 'rb_tech_portfolio.TechnologiesPage'),

这一切都很好。但是我想做的是列出到这些引用页面的链接,但是我似乎无法找出实现此目的的最佳方法。我得到的最接近的是{% for technology in page.technologies.all %},但这只是给我连接两个页面模型的对象,而我想要引用的对象。这是否已准备就绪,或者我需要在def_context中进行额外的查询才能做到这一点?

谢谢,丹


技术内嵌面板

class ArticlesPageTechnologies(Orderable):
    page = ParentalKey(ArticlesPage, on_delete=models.CASCADE, related_name='technologies')

    technologies = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    panels = [
        PageChooserPanel('technologies', 'rb_tech_portfolio.TechnologiesPage'),
    ]

文章页面模型

class ArticlesPage(Page):
    body = RichTextField(blank=True)
    thumbnail = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
        ImageChooserPanel('thumbnail'),
        InlinePanel('technologies', label="Technologies"),
    ]

    def get_context(self, request, *args, **kwargs):
        """Adding custom stuff to our context."""
        context = super().get_context(request, *args, **kwargs)
        # Get all posts
        posts = ArticlesPage.objects.live().public().order_by('-date')[:5]

        context["posts"] = posts
        return context

文章页面模板

{% extends "base.html" %}

{% block body_class %}template-article{% endblock %}

{% block content %}
{% comment %}
{% for post in posts %}
{{ post.url }}
{% endfor %}
{% endcomment %}

<div class="row">
  <div class="col-9 col-12-medium">
    <section>
      <header>
        <h1>{{ page.title }}</h1>
      </header>
      {{ page.body|safe }}
    </section>
    </ul>
  </div>
  <div class="col-3 col-12-medium">

    <!-- Sidebar -->
    <section>

      <header>
        <h2>Technologies</h2>
      </header>

      <ul class="link-list">
        {% for technology in page.technologies.all %}
          <li>{{ technology.select_related.title }}</li>
        {% endfor %}
      </ul>

    </section>

  </div>
</div>

{% endblock content %}
django wagtail
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.