2012-05-14 48 views
2

为什么我得到一个NameError:global name 'send_mail' is not definedNameError django send_mail

从我的models.py:

from django.template import Context, loader 
from django.utils.translation import ugettext as _ 
from django.core.mail import send_mail 
....... 
class Payment(models.Model):  

    # send email for payment 
    # 1. render context to email template 
    email_template = loader.get_template('classifieds/email/payment.txt') 
    context = Context({'payment': self}) 
    email_contents = email_template.render(context) 

    # 2. send email 
    send_mail(_('Your payment has been processed.'), 
       email_contents, settings.FROM_EMAIL, 
       [self.ad.user.email], fail_silently=False) 

谢谢!

Traceback: 
File "/home/.../webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response 
    111.       response = callback(request, *callback_args, **callback_kwargs) 
File "/home/.../webapps/django/lib/python2.7/django/views/decorators/csrf.py" in wrapped_view 
    77.   return view_func(*args, **kwargs) 
File "/home/.../webapps/django/myproject/classifieds/views/create.py" in checkout 
    122.  send_mail(_('Your ad will be posted shortly.'), 

Exception Type: NameError at /checkout/2 
Exception Value: global name 'send_mail' is not defined 
+0

你可以请发布完整的堆栈跟踪? – zallarak

回答

2

追溯是从你的意见。

File "/home/.../webapps/django/myproject/classifieds/views/create.py" in checkout 
    122.  send_mail(_('Your ad will be posted shortly.'), 

您从模型发布代码。

classifieds/views/create.py没有send_mail导入,我会想。

另一个问题,虽然是为什么你在你的模型类,而不是..在模型方法做这个东西...

编辑:你把它放在这使得它看起来超级怪异的问题的方式这个发送邮件的东西正在上课,而不是方法。纵观源,你看,这是在Payment.complete方法:https://github.com/saebyn/django-classifieds/blob/master/classifieds/models.py#L273

send_mail用在这里: https://github.com/saebyn/django-classifieds/blob/master/classifieds/views/create.py#L116

但不导入,导入。

+0

https://github.com/saebyn/django-classifieds/blob/master/classifieds/models.py#L273它是在那里的模型方法。 –

0

尝试用更换您的send_mail功能:

django.core.mail.send_mail(your_parameters) 

此外,在send_mail你的第一个参数似乎并不一定是字符串,改为如下:(HTTPS://docs.djangoproject。 com/en/dev/topics/email /?from = olddocs)

send_mail('Your payment has been processed.', 
       email_contents, settings.FROM_EMAIL, 
       [self.ad.user.email], fail_silently=False) 

试试这两个建议,让我知道你得到了什么。

+0

“send_mail”的第一个参数是一个字符串,它只是传递给'ugettext'进行翻译。 –