我尝试使用 flask-cloudy 上传文件,但我无法导入存储(flask-cloudy 中的模型)这是我配置它的方式:
class DevelopmentConfig():
DEBUG = True
SECRET_KEY = 'I_will_never_told_you'
STORAGE_PROVIDER = "LOCAL"
STORAGE_CONTAINER = "./data"
STORAGE_KEY = ""
STORAGE_SECRET = ""
STORAGE_SERVER = True
我在 app/ init .py 中初始化它:
from flask import Flask, redirect, request, url_for, g
from .main import main as main_blueprint
from .auth import auth as auth_blueprint
from config import DevelopmentConfig
from flask_login import LoginManager
from flask_cloudy import Storage #import Storage
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'
def create_app():
app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
app.register_blueprint(main_blueprint)
app.register_blueprint(auth_blueprint, url_prefix='/auth')
login_manager.init_app(app)
storage = Storage()
storage.init_app(app) #initialise it
@login_manager.user_loader
def load_user(user_id):
from .models import User
try:
user = User.get(User.id == int(user_id))
except User.DoesNotExist:
user = None
return user
@login_manager.unauthorized_handler
def unauthorized_callback():
return redirect(url_for('auth.login'))
return app
然后我尝试在 app/main/views.py 中导入存储:我尝试过
from app import storage
和
from .. import storage
和
from ..__init__ import storage
但是当我使用它时它仍然不能很好地工作from app import storage
并且from .. import storage
它是这样长大的:
Traceback (most recent call last):
File "run.py", line 1, in <module>
from app import create_app
File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 2, in <module>
from .main import main as main_blueprint
File "/home/lc/workspace/homeworkAdminSystem/app/main/__init__.py", line 5, in <module>
from . import views, errors
File "/home/lc/workspace/homeworkAdminSystem/app/main/views.py", line 5, in <module>
from .. import storage
ImportError: cannot import name storage
当我尝试from ..__init__ import storage
它出现时:
Traceback (most recent call last):
File "run.py", line 1, in <module>
from app import create_app
File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 2, in <module>
from .main import main as main_blueprint
File "/home/lc/workspace/homeworkAdminSystem/app/main/__init__.py", line 5, in <module>
from . import views, errors
File "/home/lc/workspace/homeworkAdminSystem/app/main/views.py", line 6, in <module>
from ..__init__ import storage
File "/home/lc/workspace/homeworkAdminSystem/app/__init__.py", line 3, in <module>
from .auth import auth as auth_blueprint
File "/home/lc/workspace/homeworkAdminSystem/app/auth/__init__.py", line 5, in <module>
from . import views
File "/home/lc/workspace/homeworkAdminSystem/app/auth/views.py", line 4, in <module>
from ..models import db, User, Homework, Score
File "/home/lc/workspace/homeworkAdminSystem/app/models.py", line 3, in <module>
from .__init__ import login_manager
ImportError: cannot import name login_manager
这是我使用的模型(应用程序/模型),from .__init__ import login_manager
但效果很好,所以我对此感到困惑,那么如何在我的views.py中导入存储?