How add a contact form on homepage wagtail

Viewed 696

Hello I am beginner learning wagtail and Django, on my design the contact form is on the homepage and only I founded tutorial for beginners to include a contact form in a dedicated contact page, how I can include a contact form on my homepage?

My model homepage looks like this:

class HomePage(Page):
    description = models.CharField(max_length=255, blank=True,) 
    content_panels = Page.content_panels + [ FieldPanel('description', classname="full") ]

in a tutorial they use this model:

class FormField(AbstractFormField):
    page = ParentalKey('FormPage', related_name='custom_form_fields') 

class FormPage(AbstractEmailForm): 
    thank_you_text = RichTextField(blank=True)
    content_panels = AbstractEmailForm.content_panels + [ 
        InlinePanel('custom_form_fields', label="Form fields"), 
        FieldPanel('thank_you_text', classname="full"), 
        MultiFieldPanel([ 
            FieldRowPanel([ 
                FieldPanel('from_address', classname="col6"), 
                FieldPanel('to_address', classname="col6"), 
            ]), 
            FieldPanel('subject'), 
        ], "Email Notification Config"),
    ]

    def get_form_fields(self): 
        return self.custom_form_fields.all()

but this creates another page that I don't need. How I can add the form on my homepage?

Thank you very much.

1 Answers

(Untested) Instead of this from the tutorial:

class FormField(AbstractFormField):
    page = ParentalKey('FormPage', related_name='custom_form_fields')

do:

class FormField(AbstractFormField):
    page = ParentalKey('HomePage', related_name='custom_form_fields')

and then change the HomePage definition to:

class HomePage(AbstractEmailForm, Page):
    description = models.CharField(max_length=255, blank=True,) 
    thank_you_text = RichTextField(blank=True)
    content_panels = AbstractEmailForm.content_panels + Page.content_panels + [ 
        FieldPanel('description', classname="full"),
        InlinePanel('custom_form_fields', label="Form fields"), 
        FieldPanel('thank_you_text', classname="full"), 
        MultiFieldPanel([ 
            FieldRowPanel([ 
                FieldPanel('from_address', classname="col6"), 
                FieldPanel('to_address', classname="col6"), 
            ]), 
            FieldPanel('subject'), 
        ], "Email Notification Config"),
    ]

    def get_form_fields(self): 
        return self.custom_form_fields.all()
Related