Python Wagtail:在应用程序之外访问数据模型

Python Wagtail:在应用程序之外访问数据模型,python,django,wagtail,Python,Django,Wagtail,真的很简单。但我对django还不熟悉,更进一步说,我是wagtail。 我想在主页上或任何其他django应用程序中显示来自某个应用程序的帖子 当前应用程序结构: website/home/ website/blog/ website/portfolio/ 目前,我可以在“公文包索引页面”上的一个漂亮的for循环中轻松显示来自“公文包项目”的帖子 当我尝试将其扩展到主页时,没有任何效果 如何从一个应用程序访问另一个应用程序的模型等(在本例中,从“主页”上的“公文包项目”渲染数据) Portf

真的很简单。但我对django还不熟悉,更进一步说,我是wagtail。 我想在主页上或任何其他django应用程序中显示来自某个应用程序的帖子

当前应用程序结构:

website/home/
website/blog/
website/portfolio/
目前,我可以在“公文包索引页面”上的一个漂亮的for循环中轻松显示来自“公文包项目”的帖子

当我尝试将其扩展到主页时,没有任何效果

如何从一个应用程序访问另一个应用程序的模型等(在本例中,从“主页”上的“公文包项目”渲染数据)

Portfolio models.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 %}
{%extends“home/base.html”%}
{%加载静态wagtailcore_标签wagtailroutablepage_标签wagtailimages_标签%}
{%block body_class%}模板主页{%endblock%}
{%block home_content%}
{{page.title}}
{page.get_children.specific%}
{{page.get_children.title}
{%if page.feature_image%}
{%image page.feature_image fill-800x450作为feature_image%}
{%endif%}
{%endfor%}

{%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 %}
{%extends“portfolio/base.html”%}
{%load static i18n wagtailcore_tags wagtailroutable页面_tags wagtailuserbar wagtailmages_tags%}
{%block body_class%}组合项{%endblock%}
{%block公文包_content%}
{{page.title}}

{{page.description}}
{{page.body | richtext}}
{{{%image page.feature_image original class=“feature_image”%} {对于最新的投资组合中的投资组合,%}
  • {%endfor%}

    {%endblock公文包_content%}
    如果你能帮助我,我将非常感激。如果你能提供一些资源来更好地理解这些概念,请提供。我已经通过了一些教程来达到这个阶段,但我现在被卡住了


    干杯

    您可以更新
    主页
    上下文
    字典页面,将
    最新的\u公文包\u列表
    传递到其模板

    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\u portfolio\u列表

    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
    
    参考:

    太棒了!你让我平了心,非常感谢。我肯定会更多地阅读这个概念…现在我有了这个功能,我想知道你是否可以帮我多一点?我想显示一个项目,而不是整个列表。我试图过滤到“last”,但我得到了一个
    “PortfolioPage”对象不可编辑>错误。有什么想法吗?如果传递单个项目,您不需要模板中的循环。您可以在模板上下文中将此项目作为
    最后一个项目组合
    传递,并直接访问它
  • 再次感谢您。花了几分钟的时间来解决它,但它已经排序好了!您太棒了!