书上第六章电子邮件,其中的“在程序中集成发送电子邮件功能”一届,按照书上的内容修改代码后,运行,页面可以正常出来,但是在表单中填写新名字时,页面就出错了。
cmd中相关错误显示:
File "C:UsersTonglingDesktopwebhello.py", line 78, in index
send_email(app.config['FLASKY_ADMIN'], 'New User', 'mail/new_user', user=user)
File "C:UsersTonglingDesktopwebhello.py", line 108, in send_email
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
KeyError: 'FLASKY_MAIL_SENDER'
hello.py:
from flask_bootstrap import Bootstrap
from flask_script import Manager #把命令行解析功能添加到hello.py中
from flask import Flask
from flask import render_template
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from datetime import datetime
from flask import session
from flask import redirect
from flask import url_for
from flask import flash
from flask_sqlalchemy import SQLAlchemy
import os
from flask_script import Shell
from flask_migrate import Migrate, MigrateCommand
from flask_mail import Mail
from flask_mail import Message
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']=\
'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=True
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]'
app.config['FLASKy_MAIL_SENDER'] = 'Flasky Admin <111111111@qq.com>'
app.config['FLASKY_ADMIN'] = os.environ.get('FLASKY_ADMIN')
db = SQLAlchemy(app)
mail = Mail(app)
bootstrap = Bootstrap(app)
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
class Role(db.Model):
__tablename__='roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
def __repr__(self):
return '<Role %r>' % self.name
users = db.relationship('User', backref='role',lazy='dynamic')
class User(db.Model):
__tablename__='users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
def __repr__(self):
return '<User %r>' % self.username
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
class NameForm(FlaskForm):
name = StringField('你的名字?', validators=[Required()])
suibmit = SubmitField('Submit')
@app.route('/', methods=['GET','POST'])
def index():
form = NameForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.name.data).first()
if user is None:
user = User(username = form.name.data)
db.session.add(user)
session['known'] = False
if app.config['FLASKY_ADMIN']:
send_email(app.config['FLASKY_ADMIN'], 'New User', 'mail/new_user', user=user)
else:
session['known'] = True
#old_name = session.get('name')
#if old_name is not None and old_name !=form.name.data:
# flash('看起来你不是同一个人~')
session['name'] = form.name.data
form.name.data=''
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name'), known = session.get('known', False))
@app.route('/user/<name>')
def user(name):
return render_template('user.html', name=name)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))
def send_email(to, subject, template, **kwargs):
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
mail.send(msg)
#if __name__ == '__main__':
# app.run(debug=True)
if __name__ == '__main__':
db.create_all()
manager.run()
相关的环境变量也设置了,但是始终出错!!!!!
求指导?????
上に書いたことはそうです
FLASKy_MAIL_SENDER
、下に書いたことはFLASKY_MAIL_SENDER