我有这个网站,用户可以填写表格,当他们点击提交时,表格的所有结果都会通过电子邮件发送给我。除了从下拉菜单中选择之外,表单的所有部分都会被发送。有谁知道我的代码出错了? 这是我的forms.py
CONDOS=[
('silvermountaincondo', 'Silver Mountain Condo'),
('redhillcondo', 'Red Hill Condo'),
('lakesidecondo', 'Lakeside Condo'),
('pointeplacecondo', 'Pointe PLace Condo'),
('rhodesviewcondo', 'Rhodes View Condo'),
]
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True, label='Full name')
contact_email = forms.EmailField(required=True, label='Email Address')
contact_phone = forms.CharField(required=True, label='Phone number')
condo = forms.ChoiceField(required=True, label='Which condo would you like to reserve?', choices=CONDOS)
arrival_date = forms.DateField(required=True, help_text='Please enter your arrival date (YYYY-MM-DD)')
departure_date = forms.DateField(required=True, help_text='Please choose your departure date (YYYY-MM-DD)')
这是我的views.py
def contact(request):
form_class = ContactForm
if request.method =='POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = request.POST.get('contact_name', '')
contact_email = request.POST.get('contact_email','')
contact_phone = request.POST.get('contact_phone','')
condo = request.POST.getlist('condo','')
arrival_date = request.POST.get('arrival_date','')
departure_date = request.POST.get('departure_date','')
template = get_template('contact_template.txt')
content = template.render({
'contact_name': contact_name,
'contact_email': contact_email,
'contact_phone': contact_phone,
'condo': condo,
'arrival_date': arrival_date,
'departure_date': departure_date,
})
send_mail(
"New Booking from vacationcondos.vegas",
content,
"www.vacationcondos.vegas" + '',
['email@gmail.com'],
#headers = {'Reply-To': contact_email}
)
return redirect('Properties/contact')
return render(request, 'Properties/contact.html', {
'form': form_class,
})
这是电子邮件的示例
联系人姓名:John Smith
电子邮件:johnsmith@gmail.com
联络电话:5555555555
抵达日期:2017-07-30
出发日期:2017-08-13
在电话号码和到达日期之间,它应该打印从下拉列表中选择的公寓。
答案 0 :(得分:0)
在views.py文件中,condo = request.POST.getlist('condo','')
行应替换为condo = request.POST.get('condo','')
,因为您使用的是ChoiceField而不是MultipleChoiceField。然后,这应该在您的电子邮件中正确选择condo
。