Contact form/email with ajax response

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
contact.py on yout root dir of django project
========
from django import forms 
from django.utils import simplejson
from django.http import HttpResponse
from django.core.mail import EmailMessage

class ContactForm(forms.Form):
    name = forms.CharField(help_text="Your name...")
    email = forms.EmailField(help_text="Your email ...",required=True)
    message = forms.CharField(widget=forms.Textarea,required=True)

def sendform(request):
    form = ContactForm(request.POST)
    if request.POST and form.is_valid():
        message = {'successMessage':"Yeap it send!"}
        data = form.cleaned_data
        email_body = " Name %s < %s >\n Message \n %s " %( data['name'], data['email'], data['message'] )
        email = EmailMessage(
            'contact from %s' % data['name'],
            email_body,
            to=['tuemail@gmail.com'],
        )
        email.send()
    else:
        message = {
            'errorMessage':'Error!!!!',
            'execute': "$('form').innerHTML='<table>%s</table>'; " % form.as_table().replace('\n','')
        }
    return HttpResponse(
        simplejson.dumps(message),
        mimetype='text/plain'
        )

contact.html on your root dir template
====
<script src="/static/meteora/meteora.js" type="text/javascript"></script>
<script>
   Meteora.uses('Control.Form');
</script>
<form action="/contacto/sendform/" onsubmit="return formSubmit(this)" method="POST">
<fieldset>
<legend>Send a Message</legend>
<table id="form" class="form">
{{ f }}
</table>
       <input type="submit" value="Send">
</fieldset>
</form>

add it to startproject url.py
=====

from yourproject.contact import ContactForm

urlpatterns = blah blah blah

    (r'^contacto/$',
      'django.views.generic.simple.direct_to_template',
      {'template':'contact.html','extra_context':{'f':ContactForm()}}
    ),
    (r'^contacto/sendform/$','yourproject.contact.sendform' ),

More like this

  1. save form in ajax with jquery in django by ralphleyga 3 years, 8 months ago
  2. Simple Contact Form by xorl 6 years, 10 months ago
  3. ajax form handler generic view by amitu 5 years, 6 months ago
  4. Contact Form by henrikv 7 years, 1 month ago
  5. Generating an InlineModelAdmin Form on the fly by timbroder 1 year, 7 months ago

Comments

(Forgotten your password?)