我基本上是在构建一个flask Web应用程序,该应用程序使用codeforces用户ID并返回用户信息 我正在尝试向其中添加Flask-mail功能,但它会显示
错误:
TypeError:无法解包不可迭代的NoneType对象
错误在mail.send(msg)行中
from flask import Flask, render_template,redirect,request
from flask_mail import Mail,Message
import requests
import json
import os
app = Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'testdev582@gmail.com'
app.config['MAIL_PASSWORD'] = 'hardik1234'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
#print(app.config.update(mail_settings))
#app.config.update(mail_settings)
mail = Mail(app)
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST':
username = request.form['username']
email_id = request.form.get('email-id')
msg = Message("test",sender=app.config.get("MAIL_USERNAME"),recipients=[email_id])
mail.send(msg)
print(email_id)
#print(username)
url = 'http://codeforces.com/api/user.info?handles=' + username
wvurl = 'http://codeforces.com/api/contest.list?gym=false'
resp = requests.get(url)
r = requests.get(wvurl)
if resp and r:
user_data = json.loads(resp.text)['result']
contest_data = json.loads(r.text)['result']
for contest in contest_data:
if contest['phase'] == 'BEFORE':
break
print(contest['name'])
return render_template('show.html',user_data = user_data,contest_data = contest)
else:
print("Error in getting response")
return render_template('index.html')
return render_template('index.html')
if __name__=='__main__':
app.run(debug=True)
答案 0 :(得分:0)
您将要使用os.environ
示例:
app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER', 'my@email.com')
app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS', 'mypassword')
或设置环境变量 视窗: https://www.youtube.com/watch?v=IolxqkL7cD8 Linux: https://www.youtube.com/watch?v=5iWhQWVXosU