简体   繁体   English

如何将下拉菜单的选定值发送到django中的电子邮件

[英]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.

相关问题 Python Selenium:如何从下拉菜单中选择一个值 - Python Selenium: How do I select a value from drop down menu 如何单击使用 Selenium 隐藏的下拉菜单? - How do I click on a drop down menu that is hidden using Selenium? django中的页面刷新时,下拉菜单显示选定的项目 - drop down menu show selected item when page refreshes in django 如何在Django下拉菜单中设置文本? - How to set text in Django drop down menu? WebDriver。 蟒蛇。 我想按值将下拉菜单选项之一存储为文本,该怎么做? - WebDriver. Python. I want to store one of the option of drop-down menu by value as text, how to do it? 如何等待直到使用Selenium Python选择了特定的下拉菜单? - How can I wait until a specific drop-down menu selected using Selenium Python? Django中如何动态显示下拉菜单中的菜单名称 - how to display the menu name in drop-down menu dynamically in Django 下拉菜单未与 Django 一起显示 - Drop down menu not showing with Django 如何使 Django ModelForm 菜单项默认选中? - How do I make a Django ModelForm menu item selected by default? 我如何为带有django中带有“添加项”选项的下拉菜单的外键字段创建表单? - How would I create a form for a foreign key field that has a drop down menu with an 'add item' option in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM