gpt4 book ai didi

python - NameError Django send_mail

转载 作者:行者123 更新时间:2023-12-04 05:46:49 26 4
gpt4 key购买 nike

为什么我会收到 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

最佳答案

回溯来自您的观点。

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

但不是进口的,进口吧。

关于python - NameError Django send_mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com