【问题标题】:Issue on sending message from contact page - Django 1.6从联系页面发送消息的问题 - Django 1.6
【发布时间】:2015-09-12 15:31:42
【问题描述】:

我的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'])

有什么想法吗?

提前致谢!

【问题讨论】:

    标签: python django


    【解决方案1】:

    您没有在任何地方声明变量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:
           ...
    

    【讨论】:

    • 谢谢 Siddhart,但现在说: AssertionError: "to" argument must be a list or tuple =(
    • 我认为您使用 send_mail 的方式不正确。 send_mail('主题在这里', '这是邮件。', 'from@example.com', ['to@example.com'], fail_silently=False)
    • 字段呢?我的意思是它应该发送这些字段数据,我只是用这个替换它?
    • 我从这个答案中获取了这个代码:stackoverflow.com/questions/28400943/…
    • 超级棒!非常感谢悉达多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多