2

NameErrorが発生するのはなぜglobal name 'send_mail' is not definedですか?

私の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
4

4 に答える 4

2

トレースバックはあなたの見解からのものです。

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

モデルからコードを投稿しました。

classifieds/views/create.pysend_mailはインポートされていないと思います。

ただし、もう1つの質問は、モデルメソッドではなくモデルクラスでこのようなことを行うのはなぜかということです。

編集:あなたがそれを質問に入れる方法は、このメール送信のものがメソッドではなくクラスで起こっていることを非常に奇妙に見せます。ソースを見ると、次の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

ただし、インポートされません。インポートしてください。

于 2012-05-14T02:08:42.207 に答える
1

プロジェクト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

コントローラで使用するもの:

from django.core.mail import send_mail

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

参考のために:

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

于 2020-02-10T23:53:48.613 に答える
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)

これらの提案の両方を試して、あなたが得たものを私に知らせてください。

于 2012-05-14T01:56:33.980 に答える
0

NameError:名前'send_mail'が定義されていません

from django.core.mail import send_mail
于 2020-05-20T09:19:27.227 に答える