我想MultipleChoiceField
通过 Django 的邮件模块在电子邮件主题和消息中显示 a 的表单输出。
目前,主题列出的数据如下:
帕特里克·比森 (Patrick Beeson) 为 [u'cake', u'cheesecake'] 下的订单
这是我的表单中接受输入的部分:
class OrderForm(forms.Form):
ORDER_TYPE_CHOICES = (
('cake', 'Cake'),
('cheesecake', 'Cheesecake'),
('coffee catering', 'Coffee catering'),
('breakfast platter', 'Breakfast platter'),
('cookie platter', 'Cookie platter'),
('gourmet desserts platter', 'Gourmet desserts platter'),
)
...
order_type = forms.MultipleChoiceField(required=False, choices=ORDER_TYPE_CHOICES, widget=forms.CheckboxSelectMultiple)
这是我的观点:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from order_form.forms import OrderForm
def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
order_description = form.cleaned_data['order_description']
phone_number = form.cleaned_data['phone_number']
email_address = form.cleaned_data['email_address']
date_needed = form.cleaned_data['date_needed']
order_type = form.cleaned_data['order_type']
subject = "Order placed for %s by %s" % (order_type, name)
message = "Name: %s\nPhone number: %s\nEmail address: %s\nOrder type: %s\nOrder description: %s\nDate needed: %s" % (name, phone_number, email_address, order_type, order_description, date_needed)
from django.core.mail import send_mail
send_mail(subject, message, 'orders@business.com', ['name@gmail.com', email_address])
return HttpResponseRedirect('thanks/')
else:
form = OrderForm()
return render(request, 'order_form/order.html', {
'form': form,
})