简体   繁体   中英

NameError django send_mail

Why am I getting a NameError: global name 'send_mail' is not defined ?

From my 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)

Thank you!

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

The traceback is from your views.

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

You posted code from your models.

classifieds/views/create.py does not have send_mail imported, I would think.

Another question though is why are you doing this stuff on your model class and not .. in a model method ...

EDIT: The way you put it in the question it makes it look super-weird that this send mail stuff is happening on the class and not in a method. Looking at the source, you see that it is in the Payment.complete method: https://github.com/saebyn/django-classifieds/blob/master/classifieds/models.py#L273

send_mail is used here: https://github.com/saebyn/django-classifieds/blob/master/classifieds/views/create.py#L116

but is not imported, import it.

Use these in project settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'yourusername'
EMAIL_HOST_PASSWORD = 'YourPassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

In your controller use:

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)

For Reference:

https://sendgrid.com/docs/for-developers/sending-email/django/

Try replacing your send_mail function with:

django.core.mail.send_mail(your_parameters)

Also, your first parameter in send_mail doesn't seem to be a string, change to below: (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)

Try both of these suggestions and let me know what you get.

NameError:未定义名称“send_mail”

from django.core.mail import send_mail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM