[英]How do I send the selected value of a drop down menu to an email in django
I have this site where the user can fill out a form and when they hit submit all the results of the form get sent to me in an email. 我有这个网站,用户可以在其中填写表格,当他们点击提交时,表格的所有结果就会通过电子邮件发送给我。 All the parts of the form get sent except for their choice from a drop down menu.
表格的所有部分都会发送出去,除了从下拉菜单中选择的部分。 Does anyone know where I went wrong in my code?
有人知道我的代码在哪里出错吗? Here is my forms.py
这是我的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)')
Here is my views.py 这是我的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,
})
This is an example of what the email looks like 这是电子邮件外观的一个示例
Contact Name: John Smith 联系人姓名:约翰·史密斯
Email: johnsmith@gmail.com 电子邮件:johnsmith@gmail.com
Contact Phone Number: 5555555555 联络电话:5555555555
Arrival date: 2017-07-30 到达日期:2017-07-30
Departure date: 2017-08-13 出发日期:2017-08-13
In between the phone number and arrival date it should print the condo that was selected from the drop down list. 在电话号码和到达日期之间,它应该打印从下拉列表中选择的公寓。
In your views.py file, the line condo = request.POST.getlist('condo','')
should be replaced with condo = request.POST.get('condo','')
since you are using a ChoiceField instead of a MultipleChoiceField . 在您的views.py文件中,应将
condo = request.POST.getlist('condo','')
行替换为condo = request.POST.get('condo','')
因为您使用的是ChoiceField MultipleChoiceField的值 。 This should then place the correct choice for condo
in your email. 然后,这应该在您的电子邮件中放置
condo
的正确选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.