2012-05-14 45 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'進行翻譯。 –