Retrieve data from database for different wagtail app

312 Views Asked by At

I'm trying to create a Streamfield block that basically displays all of the posts (diagnoses) from a standard diagnoses listing page so that not only do I have a separate diagnoses page but am also able to add a diagnoses/posts block to other pages?

I've got this so far but it's not working:

Streams > blocks.py:

class DiagnosesListingBlock(blocks.StructBlock):

    title = blocks.CharBlock(
        required= True,
        defualt="Diagnoses",
        help_text = 'Text to display as title of Diagnoses section.'
    )

    data_items = blocks.IntegerBlock(
        max_length=2,
        blank=False,
        null=False,
        default="3",
    )
    
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['diagnoses'] = diagnoses.DiagnosisPage.objects.live().public()
        return context

    class Meta:
        template = "streams/disgnoses_listing_block.html"
        icon = "edit"
        label = "Diagnoses Listing Block"
        help_text = "Centered text to display on a page"

Diagnoses > model.py:

class DiagnosisPage(Page):

    template = "diagnoses/diagnosis_page.html"

    diagnosis_name = models.TextField(
        blank=True,
        max_length=100,

    )

    subtitle = models.TextField(
        blank=True,
        max_length=500,

    )    
    
    diagnosis_intro = models.TextField(
        blank=True,
        max_length=500
    )

    image = models.ForeignKey(
        'wagtailimages.Image',
        blank=False,
        null=True,
        on_delete=models.SET_NULL,
        help_text='This image will be used on the service listing page and will be cropped to 570px by 370px.',
        related_name='+',
    )

    summary =  models.CharField(
        max_length=160,
        blank=False,
        null=False,
        default='Curabitur pulvinar euismod ante, ac sagittis ante posuere ac. Vivamus luctus commodo dolor porta feugiat. Fusce at velit id ligula pharetra laoreet.',
        help_text='Provides an excert for the blog listing page that gives an intro to what the article is about.',
    )

    

    body = StreamField([
        ("hero_block", blocks.HeroBlock()),
        ("breadcrumb_block", blocks.BreadcrumbsBlock()),
        ("highlighted_text_block", blocks.HighlightedTextBlock()),
        ("title", blocks.TitleAndTextBlock()),
        ("about_us", blocks.AboutBlock()),
        ("cards", blocks.CardsBlock()),
        ("icon_boxes_one", blocks.TextBoxesOneBlock()),
        ("links", blocks.SiteLinksBlock()),
        ("image_and_text", blocks.ImageAndTextBlock()),
        ("image_right_and_text", blocks.ImageRightAndTextBlock()),
        ("team_members", blocks.TeamMembersBlock()),
        ("cta_1", blocks.CallToActionOneBlock()),
        ("cta_2", blocks.CallToActionTwoBlock()),
        ("cta_3", blocks.CallToActionThreeBlock()),
        ("large_modal_block", blocks.LargeModalBlock()),
        ("counters_block", blocks.CountersBlock()),
        ("share_this_post", blocks.ShareThisPostBlock()),
        ("blockquotes", blocks.BlockquoteBlock()),
        ("accordions", blocks.AccordionBlock()),
        ("testimonial", SnippetChooserBlock(
            target_model='testimonials.Testimonial')),
        ("our_numbers", blocks.OurNumbersBlock()),
        ("progress_bars", blocks.ProgresssBarsBlock()),
        ("testimonial", SnippetChooserBlock(
            target_model='testimonials.Testimonial',
            template='streams/testimonial_block.html'
        )),
        ("pricing_table", blocks.PricingTableBlock(
            table_options=new_table_options)),
        ("logo_block", blocks.LogoBlock()),
        ("timeline_block", blocks.TimelineBlock()),
        ("richtext", blocks.RichTextWithTitleBlock()),
    ], blank=True, null=True)

    content_panels = Page.content_panels + [
        FieldPanel('diagnosis_name'),
        FieldPanel('subtitle'),
        FieldPanel('diagnosis_intro'),
        ImageChooserPanel('image'),
        StreamFieldPanel("body"),
    ]

diagnosis_listing_block.html:

{% load wagtailcore_tags wagtailimages_tags %}


<section class="p-t-100 p-b-100">
    <div class="container">
        <div class="row" style="display: table;width: 100%;">
            <div class="col-lg-12">{{ self }}
                <!--Post Carousel -->
                <h4 class="mb-4">{{ self.title }}</h4>
                <div class="carousel" data-items="{{ self.data_items }}">
                    {% for diagnosis in self.diagnoses %}
                    <!-- Post item-->
                    <div class="post-item border" style="display: table-cell;">
                        <div class="post-item-wrap">
                            <div class="post-image">
                                <a href="{{ diagnosis.url }}">
                                    {% image diagnosis.image fill-250x150 format-webp as diagnosis_img %}
                                    <img alt="{{ diagnosis.diagnosis_name }} | Dual Diagnosis Network Blog" src="{{ diagnosis_img.url }}"></a>
                                <!--<span class="post-meta-category"><a href=""></a></span>-->
                            </div>
                            <div class="post-item-description">
                                <span class="post-meta-date"><i class="fa fa-calendar-o"></i>Jan 21, 2017</span>
                                <span class="post-meta-comments"><a href="{{ diagnosis.url }}"><i class="fa fa-comments-o"></i>33
                                        Comments</a></span>
                                <h2><a href="{{ diagnosis.link.url }}">{{ diagnosis.diagnosis_name }}</a></h2>
                                <p>{{ diagnosis.summary }}</p>
                                <a href="{{ diagnosis.link.url }}" class="item-link">Read More <i class="icon-chevron-right"></i></a>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                    <!-- end: Post item-->
                </div>
                <!--end: Post Carousel -->
            </div>
        </div>
    </div>
</section>
1

There are 1 best solutions below

2
On

{% for diagnosis in self.diagnoses %} should be {% for diagnosis in diagnoses %}.

The get_context method defines new variables to be available in the template - in this case, the context['diagnoses'] = ... line defines a variable diagnoses, and that's how you refer to it in the template. self.diagnoses would mean an attribute of the block value instead, but that doesn't exist - it only has attributes title and data_items.