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

时间:2017-08-21 07:21:25

标签: python django wagtail

非常简单。但是,我是django的新手,并且通过扩展w .. 我想在主页或任何其他django应用程序中显示来自应用程序的帖子。

当前的应用结构:

website/home/
website/blog/
website/portfolio/

目前,我可以轻松地显示来自'投资组合项目的帖子'在'投资组合索引页面上的一个很好的for循环中。

当我尝试将其扩展到主页时,没有任何作用。

如何从另一个应用程序访问一个应用程序的模型等(在这种情况下,从'投资组合项目中提取数据'在主页'。

投资组合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 %}

如果你能帮助我,我将非常感激。此外,如果您可以建议一些资源来更好地理解这些概念,请执行此操作。我已经通过几个教程来完成这个阶段,但我现在卡住了!

干杯

1 个答案:

答案 0 :(得分:1)

您可以更新context页面HomePage字典,将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

参考: