Django显示属性错误

时间:2013-01-01 13:27:38

标签: python django

我想向填写django网站表单的每个用户发送邮件。在为它编写代码后,我收到以下错误。我一直试图找到错误的原因但没有成功。以下是我的代码:

AttributeError at /meek/
'NoneType' object has no attribute 'e_mail'

浏览

Subject='Welcome'
message=loader.get_template('letter.txt') 
from_email='men@men.com'


def gent_me(request):
    if request.method=='POST':
       form=GentForm(request.POST)
       if form.is_valid():
          data=form.cleaned_data
          newgent=Gent(
            pub_date=datetime.datetime.now(),
            full_name=data['full_name'],
            company_name=data['company_name'],
            services=data['services'],
            e_mail=data['e_mail'],
            address=data['address'],
            city=data['city'],
            state=data['state'],
            phone_no=data['phone_no'])
         invite=newgent.save()
         send_mail(Subject, message.render(Context()),from_email,[invite.e_mail])
         return HttpResponse('Thanks. Kindly check your mail.')
      else:
          return HttpResponse('Kindly fill form.')
   else:
       return render_to_response('ment.html',{'GentForm':GentForm},context_instance=RequestContext(request))

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

Model.save方法的返回值为'无'。因此,邀请'在invite = newgent.save()之后是None。而不是

invite=newgent.save()

你应该做

newgent.save()

然后

send_mail(Subject, message.render(Context()),from_email,[newgent.e_mail])