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, ['change@this.com'])
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 %}
|
Comments
Thanks! Saved me several hours of brain time.
#
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?
#
It wouldn't be more useful if you validate the form using is_valid method instead checking the view?
#
btw, and change the "change@this" to MANAGERS or any other default e-mail in settings, just a suggestion. =)
#
I used this code, in one of my django ecommerce project. However i was unable to know the reason why this snippet wasnt sending the mail to the site owner?
http://media.xorl.de/form.js (this link is obsolete..)
Any one? could tell me that I am wrong? gg_go ++
#