sqwale
sqwale

Reputation: 575

Django Wagtail ajax contact form

I have a contact form that appears at the bottom of every page in the footer. I simply included it in my base.html at the bottom. Now i want to figure out a way to submit it. All the examples wagtail provides is under the assumption i have an entire page dedicated to it and hence submits to itself.

This cannot work for me as its not a page.

I have written pseudo code of what I think it should look like .

def submitContact(request): 
        source_email = request.POST.get('email')
        name = request.POST.get('name')
        message = request.POST.get('message')           
        if (source_email is not None) and (name is not None) and (message is not None):
            body = "sample"
            send_mail(
                name,
                message,
                source_email,
                ['test@foobar'],
                fail_silently=False,
            )

Then my form would be something like this

<form class="form-group" role="form" method="post" action="/submitContact">
 ......
</form>

Ideally if someone could point to Wagtail resources that show how to create endpoints in models that do not inherit from the Page model and are not snippets that maintain "request" content that would be useful. Ideally what I would prefer is to log this data into contact "table" then send the email after.

What should I add to my urls.py to reroute the request with the correct context for the function to retrieve the required variables and thus send the email

Additional info

I wrapped a snippet around the footer to provide some context to it using templatetags, just putting this out there incase it adds value

See below.

@register.inclusion_tag('home/menus/footer.html', takes_context=True)
def footers(context):
    return {
        'footers': Footers.objects.first(),
        'request': context['request'],
    }

Upvotes: 1

Views: 1414

Answers (2)

Johan
Johan

Reputation: 390

Instead of trying to build it yourself, why not take a look at the already existing Form Builder of Wagtail?

It enables you to create a FormPage on which you can display a custom form and even E-mail the results.

Check out the documentation here.

Upvotes: 0

Augustin Laville
Augustin Laville

Reputation: 526

You should use {% url %} template tag.

urls.py :

from django.conf.urls import url
from yourapp.views import submitContact

urlpatterns = [
    url(r'^contact/$', submitContact, name='contact'),
]

Template :

<form class="form-group" role="form" method="post" action="{% url 'contact' %}">
 ......
</form>

Another improvement is to use Django Form.

Note : prefer lower_case_with_underscores naming style for functions. Use CamelCase for classes. See PEP8 for more information.

Upvotes: 2

Related Questions