Login

Simple Contact Form

Author:
xorl
Posted:
June 3, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

Simple contact form, using a javascript form checker which you can use any you want to you can just modify the form in the way you want to.

The form checker I use I include in the 'base.html' page before the </head> section end. You can use it freely. http://media.xorl.de/form.js . A lot of this code is comprised of other peoples forms that didn't suit the simple purpose of a really basic contact form which I wanted, so I rebuilt it up from bits and pieces to make a simple basic form.

  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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
########## models.py #############
from django import newforms as forms
from django.newforms.widgets import *
from django.core.mail import send_mail, BadHeaderError

# A simple contact form with four fields.
class ContactForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    topic = forms.CharField()
    message = forms.CharField(widget=Textarea())

########## views.py ##############

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from contacts.models import ContactForm
from django.template import RequestContext, Context
from django import newforms as forms
from django.newforms.widgets import *
from django.core.mail import send_mail, BadHeaderError

def contactview(request):
		subject = request.POST.get('topic', '')
		message = request.POST.get('message', '')
		from_email = request.POST.get('email', '')

		if subject and message and from_email:
		        try:
					send_mail(subject, message, from_email, ['[email protected]'])
        		except BadHeaderError:
            			return HttpResponse('Invalid header found.')
        		return HttpResponseRedirect('/contact/thankyou/')
		else:
			return render_to_response('contacts.html', {'form': ContactForm()})
	
		return render_to_response('contacts.html', {'form': ContactForm()},
			RequestContext(request))

def thankyou(request):
		return render_to_response('thankyou.html')


########## settings.py ##############
Under urlpatterns --
    (r'^contact/thankyou/', 'contacts.views.thankyou'),
    (r'^contact/', 'contacts.views.contactview'),

########## contacts.html ############

{% extends "base.html" %}

{% block fulltitle %}Contact me!{% block title %}{% endblock %}{% endblock %}

{% block header %}
<h3> header of course</h3>
{% endblock %}

{% block extrahead %}
{% endblock %}

{% block content-wrap %}
        <div id="contact_wrap">

        <div id="contact" class="clearfix">
            <div id="c_form">
                <br /><br />
                <form name="theform" action="." method="post" id="theform">
                    <div id="name_email">

                        <label id="id_name">Name:</label>
                        <input type="text" name="name" value="" id="name" />
                        <label id="id_email">Email:</label>
                        <input type="text" name="email" value="" id="Email" />
                    </div><!-- end name_email -->
                    <div id="message">
                        <label id="id_message">Message:</label>
                        <textarea name="message" rows="8" cols="37"></textarea>

                    </div><!-- end message -->
                    <div id="submit">
                        <input type="image" src="http://path/to/submitbutton.png" onclick="YY_checkform('theform','name','#q','0','Field \'name\' is empty.','Email','#S','2','Field \'Email\' appears not to be valid.','message','2','1','Field \'message\' is empty.');return document.MM_returnValue" />
                    </div><!-- end submit -->
                    <input class="hidden" type="hidden" name="topic" value="Home : Contact Form Submission" id="subject" />
                </form>

            </div><!-- end c_form -->
        </div><!-- end contact -->

    </div><!-- end contact_wrap -->

{% endblock %}


########### thankyou.html #############

{% extends 'base.html' %}
{% block billboard %}Services{% endblock %}
{% block title %}Services{% endblock %}

{% block header %}
<h3> header of course</h3>
{% endblock %}

{% block content %}
<h3>Contact Information</h3>

<p>Thank you for contacting us.</p>

<p>We will attempt to get back to you within 48 hours</p>



{% endblock %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

uptimebox (on June 27, 2007):

Thanks! Saved me several hours of brain time.

#

Kurdy (on November 19, 2007):

Very useful. Mostly for pointing out where to place which code. Being a newbie and trying to get the whole picture :-)

Btw, shouldn't the "url re's" be in urls.py instead of settings.py?

#

curaloucura (on November 22, 2007):

It wouldn't be more useful if you validate the form using is_valid method instead checking the view?

#

curaloucura (on November 22, 2007):

btw, and change the "change@this" to MANAGERS or any other default e-mail in settings, just a suggestion. =)

#

devanshu (on December 23, 2014):

can somebody tell me what is should be kept in the base.html(the link given above is expired) In may case i just kept a blank base.html but the contacts.html is not loading so no form is being shown,also when i put up some alerts/text in the base.html they are showing up but nothing is contacts.html is loading.(all files in templates folder and no error shown)

#

Please login first before commenting.