Hello there
I'm trying to make this as easy as possible, but it's pretty
complicated.
Here's my form
class EventBookForm(ModelForm):
"""A form for governors to email to govwales with details of which
events they wish to attend"""
title = CharField()
firstname = CharField()
surname = CharField()
address = CharField(widget=Textarea(attrs={'rows': 5, 'cols': 80}))
postcode = CharField()
telephone = CharField()
email = EmailField()
govtype = Field(widget=Select(choices=GOVTYPELIST))
schoolname = CharField(widget=Textarea(attrs={'rows': 2, 'cols':
60}))
authority = CharField()
workshops = MultipleChoiceField()
requirements = CharField(widget=Textarea(attrs={'rows': 5, 'cols':
80}))
class Meta:
model = Event
def __init__(self, *args, **kwargs):
super(EventBookForm, self).__init__(*args, **kwargs)
workshop_choices = []
data = Workshop.objects.filter(evt_workshop='80')
print data
for d in data:
workshop_choices.append((d.title,d.title))
self.fields['workshops']=MultipleChoiceField(widget=CheckboxSelectMultiple,
choices=workshop_choices, required=False)
As you can see I've hard coded the id of the event into this section
data = Workshop.objects.filter(evt_workshop='80')
If I could pass the request object into the form, or the event ID
itself,I could get at the right data for the workshop_choices list.
Here's the view
Note that I still haven't worked out how I send only the selected/
ticked multiple items yet
def event_book(request,edate, eslug):
if request.method == 'POST':
form = EventBookForm(request.POST)
if form.is_valid():
the_rufurl = request.META['HTTP_REFERER']
the_url = request.META['HTTP_REFERER'].split('/')
eventdate = "%s-%s-%s" % (the_url[5], the_url[6], the_url[7])
eventslug = the_url[8]
eventdetails = Event.objects.filter(start_date=eventdate)
for d in eventdetails:
eventid =
d.id
eventtitle = d.title
print "event title" + eventtitle
eventdate = d.start_date
eventstarttime = d.start_time
eventendtime = d.end_time
eventvenue = d.venue
workshops = Workshop.objects.filter(evt_workshop=
d.id)
workshoptitlearray = ''
for w in workshops:
workshoptitle = w.title
workshoptitlearray = workshoptitlearray + ', ' + w.title
to = ['
ton...@gmail.com','
con...@governorswales.org.uk']
subject = 'Event Booking Form'
from_email = '
con...@governorswales.org.uk'
cleaned_data = form.cleaned_data
print cleaned_data['workshop']
htmllinksuggestion = 'Title: ' + cleaned_data['title'] + '<br />'
htmllinksuggestion += 'First Name: ' + cleaned_data['firstname'] +
'<br />'
htmllinksuggestion += 'Surname: ' + cleaned_data['surname'] + '<br /
>'
htmllinksuggestion += 'Address: ' + cleaned_data['address'] + '<br /
>'
htmllinksuggestion += 'Post Code: ' + cleaned_data['postcode'] +
'<br />'
htmllinksuggestion += 'Telephone Number: ' +
cleaned_data['telephone'] + '<br />'
htmllinksuggestion += 'Email Address: ' + cleaned_data['email'] +
'<br />'
htmllinksuggestion += 'Type of Governor: ' +
cleaned_data['govtype'] + '<br />'
htmllinksuggestion += 'Name of School: ' +
cleaned_data['schoolname'] + '<br />'
htmllinksuggestion += 'Authority: ' + cleaned_data['authority'] +
'<br />'
htmllinksuggestion += 'Event Title: ' + str(eventtitle) + '<br />'
htmllinksuggestion += 'Event Date: ' + str(eventdate) + '<br />'
htmllinksuggestion += 'Workshops Selected: ' +
str(workshoptitlearray) + '<br />'
htmllinksuggestion += 'Requirements: ' +
cleaned_data['requirements'] + '<br />'
html_content = htmllinksuggestion
text_content = ''
msg = EmailMultiAlternatives(subject, text_content, from_email, to,
headers = {'Reply-To': '
mjda...@creativebeans.co.uk'})
msg.attach_alternative(html_content, "text/html")
msg.send()
return HttpResponseRedirect('/forms/eventbook/thankyou/')
else:
form = EventBookForm()
the_refurl = request.META['HTTP_REFERER']
the_url = request.META['HTTP_REFERER'].split('/')
if the_url[3] == 'events':
ventdate = the_url[4] + '-' + the_url[5] + '-' + the_url[6]
ventslug = the_url[7]
else:
ventdate = "%s-%s-%s" % (the_url[5], the_url[6], the_url[7])
ventslug = the_url[8]
eventdate = "%s-%s-%s" % (the_url[5], the_url[6], the_url[7])
eventdetail = Event.objects.filter(slug=ventslug,start_date=ventdate)
for d in eventdetail:
workshopid =
d.id
workshop = Workshop.objects.filter(evt_workshop=workshopid)
cats = formextra.objects.all()
context = Context({'form': form, 'cats': cats, 'eventdetail':
eventdetail, 'workshop': workshop })
return render_to_response('forms/eventbookform.html', context,
context_instance=RequestContext(request))
Any help would be gratefully received.