Wagtail:访问应用程序外部的数据模型
Wagtail: accessing data models outside of an app
真的很简单。但我是 django 的新手,而且是 wagtail。
我想在主页上或任何其他 django 应用程序中显示来自某个应用程序的帖子。
当前应用结构:
website/home/
website/blog/
website/portfolio/
目前我可以在 'portfolio index page'.
上以一个很好的 for 循环轻松显示来自 'portfolio items' 的帖子
当我尝试将其扩展到主页时,没有任何效果。
如何从一个应用程序访问另一个应用程序的模型等(在本例中,从 'portfolio items' 渲染数据到 'home'。
投资组合modelss.py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django import forms, utils
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, PageChooserPanel
from wagtail.wagtailimages.models import Image
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.tags import ClusterTaggableManager
from taggit.models import TaggedItemBase, Tag as TaggitTag
class PortfolioHome(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full"),
]
class PortfolioPage(Page):
body = RichTextField(blank=True)
feature_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
categories = ParentalManyToManyField('portfolio.PortfolioPageCategory', blank=True)
tags = ClusterTaggableManager(through='portfolio.PortfolioPageTag', blank=True)
pub_date = utils.timezone.now()
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
FieldPanel('tags'),
ImageChooserPanel('feature_image'),
]
class PortfolioPageTag(TaggedItemBase):
content_object = ParentalKey('PortfolioPage', related_name='portfolio_tags')
@register_snippet
class Tag(TaggitTag):
class Meta:
proxy = True
verbose_name = "Tag portfolio"
verbose_name_plural = "Tags portfolio"
@register_snippet
class PortfolioPageCategory(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=80)
panels = [
FieldPanel('name'),
FieldPanel('slug'),
]
def __str__(self):
return self.name
class Meta:
verbose_name = "Category portfolio"
verbose_name_plural = "Categories portfolio"
首页models.py:
from __future__ import unicode_literals
from django.db import models
from django import forms
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, PageChooserPanel
from portfolio.models import PortfolioPage
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="homepage"),
]
Home_page.html 模板:
{% extends "home/base.html" %}
{% load static wagtailcore_tags wagtailroutablepage_tags wagtailimages_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block home_content %}
<h1>{{ page.title }}</h1>
<div class="content">
{% for page in page.get_children.specific %}
<h2>{{ page.get_children.title }}</h2>
<h2><a href="{% pageurl page %}">{{ page.title }}</a></h2>
{% if page.feature_image %}
<section>
<span class="image featured">
{% image page.feature_image fill-800x450 as feature_image %}
<img alt="{{ page.feature_image.title }}" src="{{ feature_image.url }}">
</span>
</section>
{% endif %}
{% endfor %}
</div>
<p><a href="{% url 'wagtailadmin_home' %}">Wagtail admin</a></p>
{% endblock home_content %}
portfolio_home.html 模板(完美呈现所有数据):
澄清一下,我希望主页上有这种行为...
{% extends "portfolio/base.html" %}
{% load static i18n wagtailcore_tags wagtailroutablepage_tags wagtailuserbar wagtailimages_tags %}
{% block body_class %}portfolio-item{% endblock %}
{% block portfolio_content %}
{{ page.title }}
<br>
{{ page.description }}
<br>
{{ page.body|richtext }}
<br>
{# {% image page.feature_image original class="feature_image" %} #}
{% for portfolio in latest_portfolio_list %}
<li><a href="/first_app/{{ portfolio.id }}/">{{ portfolio.title }}</a></li>
{% endfor %}
<p><a href="{{ page.get_parent.url }}">Return to portfolio</a></p>
{% endblock portfolio_content %}
如果您能提供帮助,我将不胜感激。此外,如果您可以建议一些资源以更好地理解这些概念,请这样做。我已经通过几个教程进入了这个阶段,但我现在被卡住了!
干杯
您可以更新 HomePage
页面的 context
字典以将 latest_portfolio_list
传递给它的模板。
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="homepage"),
]
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['latest_portfolio_list'] = PortfolioPage.objects.live()
return context
然后在您的主页模板中循环 lastest_portfolio_list
。
参考:
真的很简单。但我是 django 的新手,而且是 wagtail。 我想在主页上或任何其他 django 应用程序中显示来自某个应用程序的帖子。
当前应用结构:
website/home/
website/blog/
website/portfolio/
目前我可以在 'portfolio index page'.
上以一个很好的 for 循环轻松显示来自 'portfolio items' 的帖子当我尝试将其扩展到主页时,没有任何效果。
如何从一个应用程序访问另一个应用程序的模型等(在本例中,从 'portfolio items' 渲染数据到 'home'。
投资组合modelss.py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django import forms, utils
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, PageChooserPanel
from wagtail.wagtailimages.models import Image
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.tags import ClusterTaggableManager
from taggit.models import TaggedItemBase, Tag as TaggitTag
class PortfolioHome(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full"),
]
class PortfolioPage(Page):
body = RichTextField(blank=True)
feature_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
categories = ParentalManyToManyField('portfolio.PortfolioPageCategory', blank=True)
tags = ClusterTaggableManager(through='portfolio.PortfolioPageTag', blank=True)
pub_date = utils.timezone.now()
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
FieldPanel('tags'),
ImageChooserPanel('feature_image'),
]
class PortfolioPageTag(TaggedItemBase):
content_object = ParentalKey('PortfolioPage', related_name='portfolio_tags')
@register_snippet
class Tag(TaggitTag):
class Meta:
proxy = True
verbose_name = "Tag portfolio"
verbose_name_plural = "Tags portfolio"
@register_snippet
class PortfolioPageCategory(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=80)
panels = [
FieldPanel('name'),
FieldPanel('slug'),
]
def __str__(self):
return self.name
class Meta:
verbose_name = "Category portfolio"
verbose_name_plural = "Categories portfolio"
首页models.py:
from __future__ import unicode_literals
from django.db import models
from django import forms
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, PageChooserPanel
from portfolio.models import PortfolioPage
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="homepage"),
]
Home_page.html 模板:
{% extends "home/base.html" %}
{% load static wagtailcore_tags wagtailroutablepage_tags wagtailimages_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block home_content %}
<h1>{{ page.title }}</h1>
<div class="content">
{% for page in page.get_children.specific %}
<h2>{{ page.get_children.title }}</h2>
<h2><a href="{% pageurl page %}">{{ page.title }}</a></h2>
{% if page.feature_image %}
<section>
<span class="image featured">
{% image page.feature_image fill-800x450 as feature_image %}
<img alt="{{ page.feature_image.title }}" src="{{ feature_image.url }}">
</span>
</section>
{% endif %}
{% endfor %}
</div>
<p><a href="{% url 'wagtailadmin_home' %}">Wagtail admin</a></p>
{% endblock home_content %}
portfolio_home.html 模板(完美呈现所有数据): 澄清一下,我希望主页上有这种行为...
{% extends "portfolio/base.html" %}
{% load static i18n wagtailcore_tags wagtailroutablepage_tags wagtailuserbar wagtailimages_tags %}
{% block body_class %}portfolio-item{% endblock %}
{% block portfolio_content %}
{{ page.title }}
<br>
{{ page.description }}
<br>
{{ page.body|richtext }}
<br>
{# {% image page.feature_image original class="feature_image" %} #}
{% for portfolio in latest_portfolio_list %}
<li><a href="/first_app/{{ portfolio.id }}/">{{ portfolio.title }}</a></li>
{% endfor %}
<p><a href="{{ page.get_parent.url }}">Return to portfolio</a></p>
{% endblock portfolio_content %}
如果您能提供帮助,我将不胜感激。此外,如果您可以建议一些资源以更好地理解这些概念,请这样做。我已经通过几个教程进入了这个阶段,但我现在被卡住了!
干杯
您可以更新 HomePage
页面的 context
字典以将 latest_portfolio_list
传递给它的模板。
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="homepage"),
]
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['latest_portfolio_list'] = PortfolioPage.objects.live()
return context
然后在您的主页模板中循环 lastest_portfolio_list
。