Thanks for the fast reply …
forms.py
from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField()
contact_email = forms.EmailField()
content = forms.CharField(widget=forms.Textarea)
# adding some better label names
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = "Your name:"
self.fields['contact_email'].label = "Your email:"
self.fields['content'].label = "What do you want to say?"
views.py
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from the_start.forms import ContactForm
from django.shortcuts import render, redirect
from the_start.models import Thing
# Create your views here.
def index(request):
things = Thing.objects.all()
# this is your new view
return render(request, 'index.html', {'things': things,})
def thing_detail(request, slug):
# grab the object
thing = Thing.objects.get(slug=slug)
# and pass to the template
return render(request, 'things/thing_detail.html', { 'thing': thing, })
def browse_by_name(request, initial=None):
if initial:
things = Thing.objects.filter(name__istartswith=initial).order_by('name')
else:
things = Thing.objects.all().order_by('name')
return render(request, 'search/search.html', { 'things': things, 'initial': initial, })
def contact(request):
form_class = ContactForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = form.cleaned_data['contact_name']
contact_email = form.cleaned_data['contact_email']
form_content = form.cleaned_data['content']
# email the profile with the contact info
template = get_template('contact_template.txt')
context = Context({ 'contact_name' : contact_name, 'contact_email' : contact_email, 'form_content' : form_content, })
content = template.render(context)
email = EmailMessage('New Contact form submission', content, 'Your website <[email protected]>', ['[email protected]'], headers = {'Reply-To': contact_email })
email.send()
return redirect('contact')
return render(request, 'contact.html', {'form': form_class, })