我在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'])
关于这可能是什么的任何想法?
提前致谢!
答案 0 :(得分:1)
您没有在任何地方声明变量first_name
,last_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:
...