From fc5ad68e62bd2b82450cdbdd3c83f8c1e94feed2 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 4 Dec 2012 15:50:42 -0500 Subject: Add contact form --- codemash/settings.py | 8 ++++++++ codemash/urls.py | 3 +-- contact/__init__.py | 0 contact/forms.py | 7 +++++++ contact/models.py | 3 +++ contact/tests.py | 16 ++++++++++++++++ contact/urls.py | 7 +++++++ contact/views.py | 26 ++++++++++++++++++++++++++ templates/contact/form.html | 17 +++++++++++++++++ templates/contact/thanks.html | 14 ++++++++++++++ templates/index.html | 3 +++ 11 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 contact/__init__.py create mode 100644 contact/forms.py create mode 100644 contact/models.py create mode 100644 contact/tests.py create mode 100644 contact/urls.py create mode 100644 contact/views.py create mode 100644 templates/contact/form.html create mode 100644 templates/contact/thanks.html diff --git a/codemash/settings.py b/codemash/settings.py index 3bf0f6a..23b1b93 100644 --- a/codemash/settings.py +++ b/codemash/settings.py @@ -114,6 +114,12 @@ TEMPLATE_DIRS = ( os.path.join(PROJECT_ROOT, "templates"), ) +# The email backend to use. For possible shortcuts see django.core.mail. +# The default is to use the SMTP backend. +# Third-party backends can be specified by providing a Python path +# to a module that defines an EmailBackend class. +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' + INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', @@ -125,6 +131,8 @@ INSTALLED_APPS = ( # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', + + 'contact', ) # A sample logging configuration. The only tangible logging diff --git a/codemash/urls.py b/codemash/urls.py index 2fae4f3..0431c65 100644 --- a/codemash/urls.py +++ b/codemash/urls.py @@ -5,9 +5,8 @@ from django.conf.urls import patterns, include, url # admin.autodiscover() urlpatterns = patterns('', - # Examples: + (r'^contact/', include('contact.urls', namespace='contact')), url(r'^$', 'pages.views.home', name='home'), - # url(r'^codemash/', include('codemash.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), diff --git a/contact/__init__.py b/contact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contact/forms.py b/contact/forms.py new file mode 100644 index 0000000..5c77b29 --- /dev/null +++ b/contact/forms.py @@ -0,0 +1,7 @@ +from django import forms + + +class ContactForm(forms.Form): + subject = forms.CharField(max_length=100) + message = forms.CharField() + sender = forms.EmailField() diff --git a/contact/models.py b/contact/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/contact/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/contact/tests.py b/contact/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/contact/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/contact/urls.py b/contact/urls.py new file mode 100644 index 0000000..0a28711 --- /dev/null +++ b/contact/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import patterns, include, url + + +urlpatterns = patterns('contact.views', + url(r'^thanks/$', 'contact_processor', name='thanks'), + url(r'^$', 'contact_form', name='form'), +) diff --git a/contact/views.py b/contact/views.py new file mode 100644 index 0000000..e3a6511 --- /dev/null +++ b/contact/views.py @@ -0,0 +1,26 @@ +from django.shortcuts import render, redirect +from django.core.mail import send_mail + +from contact.forms import ContactForm + + +def contact_form(request): + return render(request, 'contact/form.html', { + 'form': ContactForm(), + }) + + +def contact_processor(request): + if request.method != "POST": + return redirect("contact:form") + + form = ContactForm(request.POST) + if not form.is_valid(): + return redirect("contact:form") + + send_mail(form.cleaned_data['subject'], form.cleaned_data['message'], + form.cleaned_data['sender'], ['codemash@example.com']) + + return render(request, 'contact/thanks.html', { + 'form': form, + }) diff --git a/templates/contact/form.html b/templates/contact/form.html new file mode 100644 index 0000000..7ada5e0 --- /dev/null +++ b/templates/contact/form.html @@ -0,0 +1,17 @@ + + + + + + Contact Us + + + +

Contact Us

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ + diff --git a/templates/contact/thanks.html b/templates/contact/thanks.html new file mode 100644 index 0000000..67e8336 --- /dev/null +++ b/templates/contact/thanks.html @@ -0,0 +1,14 @@ + + + + + + Thank You + + + +

Thanks for contacting us

+

We have sent the following message to the organizers. Thanks.

+

{{ form.message.value }}

+ + diff --git a/templates/index.html b/templates/index.html index a522fbd..0ea840f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,6 +8,9 @@

Hello Codemash

+

You've just created your first Django page. Pretty cool, eh?

-- cgit v1.2.3