蓝图中的烧瓶邮件问题

时间:2018-07-03 20:59:46

标签: python python-3.x flask blueprint flask-mail

我正尝试在注册后向我的网站发送一封确认电子邮件。我的问题是,apps.config文件在routes.py文件中不可读。

from flask import Blueprint, render_template, redirect, request, url_for
from flask_login import (
    current_user,
    LoginManager,
    login_required,
    login_user,
    logout_user
)
from flask_mail import Mail, Message
from itsdangerous import URLSafeTimedSerializer, SignatureExpired
from .forms import LoginForm, CreateAccountForm
from flask import current_app as app


# start the login system
login_manager = LoginManager()

blueprint = Blueprint(
    'base_blueprint',
    __name__,
    url_prefix='',
    template_folder='templates',
    static_folder='static'
)



from database import db
from .models import User



@blueprint.route('/')
def route_default():
    return redirect(url_for('base_blueprint.login'))


@blueprint.route('/<template>')
@login_required
def route_template(template):
    return render_template(template + '.html')


@blueprint.route('/fixed_<template>')
@login_required
def route_fixed_template(template):
    return render_template('fixed/fixed_{}.html'.format(template))


@blueprint.route('/page_<error>')
def route_errors(error):
    return render_template('errors/page_{}.html'.format(error))

## Login & Registration
s = URLSafeTimedSerializer("secretexample")

@blueprint.route('/login', methods=['GET', 'POST'])
def login():
    login_form = LoginForm(request.form)
    create_account_form = CreateAccountForm(request.form)
    if 'login' in request.form:
        username = str(request.form['username'])
        password = str(request.form['password'])
        user = db.session.query(User).filter_by(username=username).first()
        if user and password == user.password:
            login_user(user)
            return redirect(url_for('base_blueprint.route_default'))
        return render_template('errors/page_403.html')
    elif 'create_account' in request.form:
        login_form = LoginForm(request.form)
        user = User(**request.form)
        db.session.add(user)
        db.session.commit()
        email = str(request.form['email'])
        token = s.dumps(email,salt ='examplesalt')
        msg = Message('Confirm Email', sender='examplemail@gmail.com', recipients=[email])
        link = url_for('base_blueprint.confirm_email', token=token, external=True)
        msg.body = "Please confirm email using the following link {}".format(link)
        mail.send(msg)
        return redirect(url_for('base_blueprint.login')+'#login')
    if not current_user.is_authenticated:
        return render_template(
            'login/login.html',
            login_form=login_form,
            create_account_form=create_account_form
        )
    return redirect(url_for('home_blueprint.index'))



s = URLSafeTimedSerializer('SecretSport')

@blueprint.route('/confirm_email/<token>')
def confirm_email(token):
    try:
        email = s.loads(token, salt='emailSport', max_age= 300)
    except SignatureExpired:
        return "This token has expired"
    return 'congrats for signing up'


@blueprint.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('base_blueprint.login'))


@blueprint.route('/shutdown')
def shutdown():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()
    return 'Server shutting down...'

## Errors


@login_manager.unauthorized_handler
def unauthorized_handler():
    return render_template('errors/page_403.html'), 403


@blueprint.errorhandler(403)
def access_forbidden(error):
    return render_template('errors/page_403.html'), 403


@blueprint.errorhandler(404)
def not_found_error(error):
    return render_template('errors/page_404.html'), 404


@blueprint.errorhandler(500)
def internal_error(error):
    return render_template('errors/page_500.html'), 500

我的问题是我无法在此文件中使用邮件。我使用“从烧瓶导入current_app作为应用程序”导入应用程序后尝试使用mail = Mail(app)。 mail = Mail(app)通常可以在app.py文件中使用,但是我在用于蓝图的单独文件中。我已经尝试过mail = Mail(blueprint),但是它说blueprint没有配置文件。有人可以帮我得到邮件吗?

0 个答案:

没有答案