我正在为Django使用Wagtail,并且想在模板中调用孙子,但不知道该怎么做。我是新来的,似乎在w文档中找不到任何内容。我看到了“页面查询参考”文档here,但不知道如何应用它。
我的Django项目树如下:
project/
home/
models.py
> class HomePage
news/
models.py
> class NewsPostPage
> class NewsIndexPage
在主页模板中,我想将新闻应用程序中的最新新闻插入模板。
home / models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from modelcluster.models import ParentalKey
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.models import Page
from news.models import NewsIndexPage
class HomePage(Page):
why_choose_us_title_en = models.CharField(
verbose_name=_('[EN] Why Choose Us Title'),
max_length=200,
default='',
blank=True,
)
why_choose_us_subtitle_en = models.CharField(
verbose_name=_('[EN]Why Choose Us Subtitle'),
max_length=200,
default='',
blank=True,
)
content_panels = Page.content_panels + [
FieldPanel('why_choose_us_title_en', classname='full title'),
FieldPanel('why_choose_us_subtitle_en'),
]
subpage_types = [
'news.NewsIndexPage',
]
@property
def featured_products(self):
return ProductPage.objects.filter(featured=True)
news_index = NewsIndexPage
news / models.py
from datetime import timedelta
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from wagtail.admin.edit_handlers import FieldPanel, RichTextFieldPanel
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel
from base.models import TranslatedField
class NewsPostPage(Page):
news_title_en = models.CharField(
max_length=7250,
verbose_name=_('[EN] News Post Page Title'),
blank=True,
default='',
)
news_text_en = RichTextField(
verbose_name=_('[EN] News Text'),
blank=True,
default='',
help_text=_('[EN] News content')
)
preview_text_en = RichTextField(
verbose_name=_('[EN] News Preview Text'),
blank=True,
default='',
help_text=_('[EN] Index Page News Preview Text')
)
created = models.DateField(
verbose_name=_('Release Date'),
blank=True
)
@property
def recent_posts(self):
two_months_before = timezone.now().date() - timedelta(days=60)
posts = NewsPostPage.objects.filter(created__gt=two_months_before)
return posts
content_panels = Page.content_panels + [
FieldPanel('news_title_en', classname='title full'),
RichTextFieldPanel('news_text_en'),
RichTextFieldPanel('preview_text_en'),
FieldPanel('created')
]
parent_page_types = ['NewsIndexPage']
class NewsIndexPage(Page):
news_index_title_en = models.CharField(
max_length=250,
verbose_name=_('[EN] News Index Title'),
blank=True,
default='',
)
news_index_image = models.ForeignKey(
'wagtailimages.Image',
related_name='+',
null=True,
on_delete=models.SET_NULL
)
content_panels = Page.content_panels + [
FieldPanel('news_index_title_en'),
ImageChooserPanel('news_index_image'),
]
subpage_types = ['NewsPostPage']
@property
def news_posts(self):
return self.get_children().live().specific()
@property
def recent_posts(self):
two_months_before = timezone.now().date() - timedelta(days=60)
posts = NewsPostPage.objects.filter(created__gt=two_months_before)
return posts
主页/模板
{% block content %}
<div id="kotak-news-events" class="kotak-grid">
<h4 class="title-strip title-kotak-bawah">Recent News and Events</h4>
<table>
{% for post in page.news_index.news_posts.all %}
<tr>
<td>{{ post.created }}</td>
<td>{{ post.translated_preview_text|richtext }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
我不知道如何在NewsPostPage中插入项目以使其显示在主页模板中。预先感谢
答案 0 :(得分:2)
您要执行的操作是向模板中添加其他context
。 g具有将视图和模型混合在一起的酷功能。因此,您要做的就是将@properties
添加到发送到模板的context
中。像这样:
class NewsIndexPage(Page):
template = 'templates/news_index_page.html'
@property
def news_posts(self):
return self.get_children().live()
@property
def recent_posts(self):
two_months_before = timezone.now().date() - timedelta(days=60)
posts = NewsPostPage.objects.filter(created__gt=two_months_before)
return posts
def get_context(self, request, *args, **kwargs):
"""Add posts to context"""
context = super().get_context(request)
context['recent_posts'] = self.recent_posts
context['news_posts'] = self.news_posts
return context
由于将QuerySet返回到模板,因此可以正常遍历它们。因此您的模板看起来像这样:
{% block content %}
<div id="kotak-news-events" class="kotak-grid">
<h4 class="title-strip title-kotak-bawah">Recent News and Events</h4>
<table>
{% for post in news_posts %} {# Notice the loop here #}
<tr>
<td>{{ post.created }}</td>
<td>{{ post.translated_preview_text|richtext }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
答案 1 :(得分:1)
事实证明,您只需要添加一个属性,我就可以通过添加自定义属性来解决,如下所示:
class HomePage(Page):
why_choose_us_title_en = models.CharField(
verbose_name=_('[EN] Why Choose Us Title'),
max_length=200,
default='',
blank=True,
)
why_choose_us_subtitle_en = models.CharField(
verbose_name=_('[EN]Why Choose Us Subtitle'),
max_length=200,
default='',
blank=True,
)
content_panels = Page.content_panels + [
FieldPanel('why_choose_us_title_en', classname='full title'),
FieldPanel('why_choose_us_subtitle_en'),
]
subpage_types = [
'news.NewsIndexPage',
]
@property
def featured_products(self):
return ProductPage.objects.filter(featured=True)
# removed this line
# news_index = NewsIndexPage
# added this code
@property
def news_index(self):
return NewsPostPage.objects.live()
仍然将其用于HTML
{% block content %}
<div id="kotak-news-events" class="kotak-grid">
<h4 class="title-strip title-kotak-bawah">Recent News and Events</h4>
<table>
{% for post in page.news_index.all %}
<tr>
<td>{{ post.created }}</td>
<td>{{ post.translated_preview_text|richtext }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}