comparison contact/views.py @ 819:38db6ec61af3

Contact form can prepopulate subject field now. Also updated code for PEP-8 and added tests.
author Brian Neal <bgneal@gmail.com>
date Sat, 20 Sep 2014 14:05:52 -0500
parents ee87ea74d46b
children 6ac56115e0a8
comparison
equal deleted inserted replaced
818:cf486a8e8b43 819:38db6ec61af3
1 # Create your views here. 1 """Views for the contact application."""
2 2
3 from django.shortcuts import render_to_response 3 from django.shortcuts import redirect, render
4 from django.template import RequestContext
5 from django.http import HttpResponseRedirect
6 from django.core.urlresolvers import reverse
7 4
8 from contact.forms import ContactForm 5 from contact.forms import ContactForm
9 from core.functions import get_full_name 6 from core.functions import get_full_name
10 7
11 8
12 def contact_form(request): 9 def contact_form(request):
13 if request.method == 'POST': 10 if request.method == 'POST':
14 form = ContactForm(request.POST) 11 form = ContactForm(request.POST)
15 if form.is_valid(): 12 if form.is_valid():
16 form.save() 13 form.save()
17 return HttpResponseRedirect(reverse('contact.views.contact_thanks')) 14 return redirect('contact-thanks')
18 else: 15 else:
19 initial_data = {} 16 initial_data = {}
20 if request.user.is_authenticated(): 17 if request.user.is_authenticated():
21 name = get_full_name(request.user) 18 name = get_full_name(request.user)
22 initial_data = {'name' : name, 'email' : request.user.email} 19 initial_data = {'name': name, 'email': request.user.email}
23 20
24 form = ContactForm(initial = initial_data) 21 subject = request.GET.get('subject')
22 if subject:
23 initial_data['subject'] = subject
25 24
26 return render_to_response('contact/contact_form.html', 25 form = ContactForm(initial=initial_data)
27 {'form' : form},
28 context_instance = RequestContext(request))
29 26
30 27 return render(request, 'contact/contact_form.html', {'form': form})
31 def contact_thanks(request):
32 return render_to_response('contact/contact_thanks.html',
33 context_instance = RequestContext(request))