Wagtail Index页面不显示孩子的对象

时间:2016-09-22 16:39:27

标签: django django-templates django-views wagtail

我在我的智慧结束时觉得我错过了一些简单的东西,但我一遍又一遍地看着它而无法理解它。

我有一个简单的person_index_page我想要显示孩子的person_page对象,但无论我尝试什么......都没有。我有几个网站有类似的设置,他们工作。你能看看我下面的代码,看看你是否注意到我遗漏了什么?谢谢。

home_tags.py

# Person feed for home page and staff page
@register.inclusion_tag(
    'home/tags/person_listing_homepage.html',
    takes_context=True
)
def person_listing_homepage(context, count=3):
    people = PersonPage.objects.live().order_by('?')
    return {
        'people': people[:count].select_related('feed_image'),
        'request': context['request'],
    }

person_index_page.html

{% extends 'base.html' %}
{% load wagtailcore_tags wagtailimages_tags home_tags %}

{% block content %}
...

    {% include "home/tags/person_listing_homepage.html" %}

...
{% endblock %}

person_listing_homepage.html 可能应该在某个时候命名

{% for person in people %}
  {% include "home/includes/person_list_item.html" %}
{% endfor %}

person_list_item.html

{% load wagtailcore_tags wagtailimages_tags %}

{# Individual person item in a list - used on people index and home page #}
<a class="list-group-item" href="{% pageurl person %}">
  <div class="media">
    {% if person.feed_image %}
      <div class="media-left">
        {% image person.feed_image width-200 as img %} <img class="media-object" src="{{ img.url }}"/>
      </div>
    {% endif %}
    <div class="media-body">
      <h4 class="media-heading">{{ person.first_name }} {{ person.last_name }}</h4>
      {% if person.search_description %}
        <p>{{ person.search_description }}</p>
      {% endif %}
    </div>
  </div>
</a>

1 个答案:

答案 0 :(得分:1)

这比Wagtail更像是一个“如何调试”的问题。而不是直接给你答案,这是我要采取的过程:

您说在print(people)函数中添加person_listing_homepage不会显示任何内容。所以,你的下一个问题应该是:“这个函数是否正在运行?”将print语句更改为print("GOT HERE")。你会发现它也没有显示任何东西 - 它告诉你函数没有被运行。

下一步是在调用函数的位置周围添加一些调试输出 - 如果没有显示,你知道代码没有被运行,你必须继续上升,直到找到正在运行的东西。那么让我们来寻找那个地方......

这就是你发现问题的地方。您永远不会在代码中的任何位置调用person_listing_homepage函数。您包含person_listing_homepage.html模板,但这不是一回事。 person_index_page.html应该成为:

{% extends 'base.html' %}
{% load wagtailcore_tags wagtailimages_tags home_tags %}

{% block content %}
...

    {% person_listing_homepage %}

...
{% endblock %}