Login

Contact form/email with ajax response

Author:
zodman
Posted:
August 21, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

This snippets is a simple contact form for use meteora widgets with django

For download the meteora.js and all tools http://meteora.astrata.com.mx

 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=['[email protected]'],
        )
        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. Template tag - list punctuation for a list of items by shapiromatron 11 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.