I need to create a reusable element (cta button) that I can include in many places throughout the page.
These cta buttons are used ~8 times throughout the design. How can I do this without copy-pasting?
Similar to this: Ways to create reusable sets of fields in Wagtail? Except I must be able to use the set several times on a single page.
This is what I am trying to do:
class HomePage(Page):
template = "home/home_page.html"
hero_heading = models.CharField(max_length=50)
hero_subheading = models.CharField(max_length=255)
hero_cta1 = HeroCTA1() # abstract reusable model
hero_cta2 = HeroCTA2()
content_panels = Page.content_panels + [
FieldPanel("hero_heading"),
FieldPanel("hero_subheading"),
hero_cta1.panels,
hero_cta2.panels,
]
My attempt at a reusable CTAButton class:
class CTAButton(models.Model):
text = RichTextField(max_length=25, features=["bold"])
url = models.URLField(null=True, blank=True)
page = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="%(app_label)s_%(class)s_page",
)
panels = MultiFieldPanel(
[
FieldPanel("text"),
FieldPanel("url"),
PageChooserPanel("page"),
],
heading="CTA Button Fields",
classname="collapsible",
)
class Meta:
abstract = True
class HeroCTA1(CTAButton):
pass
class HeroCTA2(CTAButton):
pass
Except this doesn't work :/ I am encountering "HomePage has no field named 'page'"
Shouldn't this break on 'text' since it's before 'page'?
Any advice on how to proceed?