【发布时间】:2020-06-20 19:43:49
【问题描述】:
尝试在烧瓶应用程序上上传图像时,我一直在尝试解决 Nameerror 'app not defined'。我很清楚这可能是一个结构性错误,但我似乎无法让它工作超过 48 小时。我在蓝图中重组了我的代码,受影响的蓝图是我的身份验证。
我尝试使用“应用程序导入应用程序”,但出现错误“无法从应用程序导入应用程序”
以下是我的代码
#app/auth/__init__.py
from flask import Blueprint
bp = Blueprint('auth', __name__)
from app.auth import views
----------------------------------------------------
app/auth/views.py
from flask import render_template, redirect, url_for, flash, request
from flask_login import login_required, login_user, logout_user, current_user
from app import db
from app.auth import bp
from app.auth.forms import LoginForm, RegistrationForm, UpdateAccountForm
from app.models import User
import os
import urllib.request
from werkzeug.utils import secure_filename
import secrets
from PIL import Image
def save_picture(form_picture):
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_picture.filename)
picture_fn = random_hex + f_ext
picture_path = os.path.join(app.root_path, 'passports', picture_fn)
output_size = (125, 125)
i = Image.open(form_picture)
i.thumbnail(output_size)
i.save(picture_path)
return picture_fn
@bp.route("/account", methods=['GET', 'POST'])
@login_required
def account():
form = UpdateAccountForm()
if form.validate_on_submit():
if form.picture.data:
picture_file = save_picture(form.picture.data)
current_user.image_file = picture_file
current_user.username = form.username.data
current_user.email = form.email.data
db.session.commit()
flash('Your account has been updated!', 'success')
return redirect(url_for('auth.account'))
elif request.method == 'GET':
form.username.data = current_user.username
form.email.data = current_user.email
image_file = url_for('static', filename='passports/' + current_user.image_file)
return render_template('auth/account.html', title='Account',
image_file=image_file, form=form)
app/__init__.py
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
import os
from flask import Flask, request, current_app
from config import Config
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_assets import Bundle, Environment
db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
#mail = Mail()
bootstrap = Bootstrap()
# Initialize the app
def create_app(config_class=Config):
app=Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)
bootstrap.init_app(app)
from app.admin import bp as admin_bp
app.register_blueprint(admin_bp, url_prefix='/admin')
from app.auth import bp as auth_bp
app.register_blueprint(auth_bp)
from app.home import bp as home_bp
app.register_blueprint(home_bp)
from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)
return app
from app import views, models
【问题讨论】:
-
或许你可以使用current_app。
-
请明确说明我如何使用它,您的意思是 current_app 导入应用程序或者您的意思是 os.path.join(current_app.app.root_path(xxxxxxxxx