I have the usual home.HomePage() model set up by wagtail by default and this is what it contains:
class HomePage(Page):
advertised_lowest_price = models.FloatField(blank=True, null=True, max_length=20,)
season_year = models.PositiveIntegerField(blank=True, null=True)
season_statement = RichTextField(blank=True, null=True, help_text="THIS MUST BE IN 'h2' TAG! To avoid frontend"" display irregularities, you must use the"" H2 tag",
features=['bold', 'italic', 'hr', 'link', 'document-link', 'embed', 'underline',
'strike-through', 'h2', 'h3'],
)
premium_topic = models.CharField(null=True, blank=True, help_text='Premium Products text', max_length=80)
premium_sub_topic = models.CharField(null=True, blank=True, help_text='Premium Products Sub-topic text',
max_length=80)
article_section_topic = models.CharField(null=True, blank=True, max_length=80)
def get_context(self, request):
context = super().get_context(request)
context['home_page'] = HomePage.objects.live().all()
def carousel_products():
carousel_items_list = []
for carousel_item in AdministratorProducts.objects.all():
carousel_items_list.append(carousel_item.is_carousel())
return carousel_items_list
context['carousel_1'] = carousel_products()[0]
return context
content_panels = Page.content_panels + [
FieldPanel('advertised_lowest_price'),
FieldPanel('season_year'),
FieldPanel('season_statement'),
FieldPanel('premium_topic'),
FieldPanel('premium_sub_topic'),
FieldPanel('article_section_topic'),
]
template = 'home/index.html'
def __str__(self):
return self.season_statement
That said,I also have a Carousel mode that serves as a housing for other products that would be on the carousel on the index page >index.html
.
This is the model for the Carousel:
class ImageCarousel(Page):
carousel_name = models.CharField(max_length=100, default='ART!')
date_time_stamp = models.DateTimeField(blank=True, null=True, help_text='Date product was added to Carousel.')
class Meta:
verbose_name_plural = 'Images on the Carousel'
def __str__(self):
return self.title
content_panels = Page.content_panels + [
FieldPanel('carousel_name'),
]
template = 'home/index.html'
As you might have noticed, ImageCarousel
also writes to the same template as HomePage
which is index.html
.
At the same time, I have another model from another app that has ImageCarousel
as a ForeignKey
. Here is the model located at 'ixorabloom_administrator_products/models.py'
:
class AdministratorProducts(Page):
administrator_product_name = models.CharField(max_length=100)
administrator_product_bio = models.TextField(blank=True, null=True)
administrator_product_primary_genre = models.CharField(max_length=4, choices=ART_GENRES, null=True, blank=True)
administrator_product_secondary_genre = models.TextField(help_text='Comma (,) separated list of secondary art ''genres',
null=True,
blank=True)
administrator_product_carousel = models.ForeignKey("home.ImageCarousel",
null=True,
blank=True,
on_delete=models.SET_NULL)
administrator_product_carousel_year = models.PositiveIntegerField(help_text="Year to be displayed if this product"" is on the carousel",
null=True,
blank=True)
administrator_product_date_of_creation = models.DateField(null=True, blank=True, help_text='Date product was made')
administrator_product_website_link = models.URLField(blank=True, null=True, help_text='External website where this''product can be found or''bought.')
administrator_product_email_address = models.EmailField(blank=True, null=True, help_text='External Mailing site''where this product can be''ordered.')
administrator_product_picture = models.ForeignKey("wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL)
administrator_product_price = models.FloatField(null=True, blank=True)
content_panels = Page.content_panels + [
FieldPanel('administrator_product_name'),
FieldPanel('administrator_product_bio'),
FieldPanel('administrator_product_primary_genre'),
FieldPanel('administrator_product_secondary_genre'),
FieldPanel('administrator_product_carousel'),
FieldPanel('administrator_product_carousel_year'),
FieldPanel('administrator_product_website_link'),
FieldPanel('administrator_product_email_address'),
ImageChooserPanel('administrator_product_picture'),
FieldPanel('administrator_product_price'),
FieldPanel('administrator_product_date_of_creation'),
]
class Meta:
verbose_name_plural = 'Administrator Art Products'
def __str__(self):
return self.administrator_product_name
def is_carousel(self):
if self.administrator_product_carousel:
return True
else:
return False
Looking back at HomePage
, there is a get_context()
method there. This method actually does a loop to gather products (from the administrator) that have a ForeignKey
relationship with ImageCarousel
. Here is the method:
def get_context(self, request):
context = super().get_context(request)
context['home_page'] = HomePage.objects.live().all()
def carousel_products():
carousel_items_list = []
for carousel_item in AdministratorProducts.objects.all():
carousel_items_list.append(carousel_item.is_carousel())
return carousel_items_list
context['carousel_1'] = carousel_products()[0]
return context
**You can also find this in ** HomePage().get_context()
.
Here is the problem. The context does not work on the front-end. I have tested the performance of the carousel_products()
function with python manage.py shell
and it gets the work done. The issue here is that it isn't showing on the webpage powered by the template: 'home/index.html'
.
Here is the section of 'home/index.html'
That makes use of the newly added context:
{% if carousel_1 %}
<div class="hs-item">
<div class="hs-left"><img src="{{ carousel_1.administrator_product_picture.url }}" alt=""></div>
<div class="hs-right">
<div class="hs-content">
<div class="price">from ₦{{ carousel_1.administrator_product_price }}</div>
<h2 style="margin-bottom: 0px;">
<span>{{ carousel_1.administrator_product_carousel_year }}</span>
<br>
</h2>
<a href="" class="site-btn">Shop NOW!</a>
</div>
</div>
</div>
{% endif %}
What am I doing wrong? Can I please also get a solution to this problem? Anything that I've not added to the question, please let me know and I will provide the required data. I also have terribly hard times with context on Wagtail CMS. I really wish someone could clearly explain their template design and use system. My implementations have all been from the Wagtail documentation. Thank you!