在我的生产服务器上,我遇到了以下错误:
"init()收到了一个意外的参数 'apikey'"
相同代码在开发服务器上是可以工作的。
我的生产服务器正在运行gunicorn,我已经向gunicorn.service文件添加了环境变量SENDGRID_API_KEY。 我已经重新启动了gunicorn和nginx。 我可以看到环境变量已被加载。
我调用发送电子邮件的方法如下:
def sendtestemail(to):
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("<myemail>@<mydomain>.com")
to_email = Email(to)
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
return [response.status_code, response.body, response.headers]
SENDGRID_API_KEY
存放在哪里?我基本上已经知道答案,但在回答之前想确认一下。 - CyberHavenProgrammingSENDGRID_API_KEY
添加到我的设置文件中,from projectroot.settings import SENDGRID_API_KEY
,然后将该变量添加到您的sg
对象中。我猜测您的os.environ
设置方式可能无法获取您的变量。在我这里,这个字典键肯定不在设置中。 - CyberHavenProgramming