繁体   English   中英

从联系页面发送消息的问题-Django 1.6

[英]Issue on sending message from contact page - Django 1.6

我在views.py上有此方法:

def contact_us(request):
form = ContactUsForm()
d = {'form': form}
if request.method == 'POST':
    form = ContactUsForm(request.POST)
    if form.is_valid():
        Contact.objects.create(
            first_name=form.cleaned_data['first_name'],
            last_name=form.cleaned_data['last_name'],
            email=form.cleaned_data['email'],
            message=form.cleaned_data['message'],
        )
        try:
            send_mail(first_name,last_name,email,message,['kristian.koci@gmail.com'])
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        d['message'] = _('Thank You! We will contact you shortly')
    else:
        d['form'] = form

return render_to_response('profiles/contact_us.html', d,
                          context_instance=RequestContext(request))

这是网址:

http://beta.contratalos.com/profiles/contact_us/

我想将该邮件发送给我在sned_mail方法中指定的收件人。

但是每次我尝试它都会引发此错误:

NameError: global name 'first_name' is not defined
File "apps/profiles/views.py", line 737, in contact_us
send_mail(first_name,last_name,email,message,['kristian.koci@gmail.com'])

有什么想法吗?

提前致谢!

您没有在任何地方声明变量first_namelast_name

以下代码将为您提供帮助。

    temp_contact=Contact.objects.create(
        first_name=form.cleaned_data['first_name'],
        last_name=form.cleaned_data['last_name'],
        email=form.cleaned_data['email'],
        message=form.cleaned_data['message'],
    )
    temp_contact.save()
    try:
       send_mail(temp_contact.first_name,temp_contact.last_name,temp_contact.email,temp_contact.message, ['kristian.koci@gmail.com'])
    except:
       ...

编辑:

以下代码将帮助您发送正确的消息。

    temp_contact=Contact.objects.create(
        first_name=form.cleaned_data['first_name'],
        last_name=form.cleaned_data['last_name'],
        email=form.cleaned_data['email'],
        message=form.cleaned_data['message'],
    )
    temp_contact.save()
    try:
        email_subject = "Contact Registration - "+temp_contact.email
        email_content = "Name:"+temp_contact.first_name+" "+temp_contact.last_name+"\nContent:"+temp_contact.message
       send_mail(email_subject,email_content,temp_contact.email, ['kristian.koci@gmail.com'])
    except:
       ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM