我有一个Flask应用程序,运行良好,直到,由于增长,我决定模块化。新结构如下:
âââ Dockerfile
âââ README.md
âââ app
â  âââ __init__.py
â  âââ config.py
â  âââ cron.py
â  âââ database.ini
â  âââ db.py
â  âââ db.sqlite3
â  âââ webapp
â  âââ __init__.py
â  âââ routes.py
âââ db.sqlite3
âââ docker-compose.yml
âââ error.log
âââ gunicorn_config.py
âââ rd.py
âââ requirements.txt
âââ static
â  âââ css
â  â  âââ bootstrap.min.css
â  âââ data.html
â  âââ fonts
â  â  âââ glyphicons-halflings-regular.eot
â  â  âââ glyphicons-halflings-regular.svg
â  â  âââ glyphicons-halflings-regular.ttf
â  â  âââ glyphicons-halflings-regular.woff
â  â  âââ glyphicons-halflings-regular.woff2
â  âââ images
â  â  âââ ajax-loader.gif
â  â  âââ gear.png
â  â  âââ gear_small.png
â  â  âââ logo.png
â  âââ index.html
â  âââ js
â  â  âââ app.js
â  â  âââ bootbox.min.js
â  â  âââ bootstrap.js
â  â  âââ bootstrap.min.js
â  â  âââ npm.js
â  âââ login.html
â  âââ manage_users.html
â  âââ register.html
â  âââ reset_password.html
â  âââ settings.html
â  âââ upload.html
âââ templates
âââ base.html
__init__.py
对于webapp:
from flask import Flask
from app.config import DebugConfig
from flask_sqlalchemy import SQLAlchemy
from importlib import import_module
from logging import basicConfig, DEBUG, getLogger, StreamHandler
import os
import uuid
def register_blueprints(app):
for module_name in (app.config['MODULES']):
module = import_module('app.{}.routes'.format(module_name))
app.register_blueprint(module.blueprint)
def configure_logs(app):
basicConfig(filename='error.log', level=DEBUG)
logger = getLogger()
logger.addHandler(StreamHandler())
def create_app():
file = (__file__)
app = Flask(__name__)
app.secret_key = str(uuid.uuid4())
app.config.from_object(DebugConfig)
register_blueprints(app)
configure_logs(app)
return app
以下是登录页路由代码:
@blueprint.route("/login", methods=["GET", "POST"])
def login():
if request.method == 'GET':
return render_template(url_for('static', filename='login.html')
很遗憾,当我尝试为应用程序提供服务时,此新结构会导致此错误:
builtins.FileNotFoundError FileNotFoundError:[Errno 2]没有这样的文件
我已经处理这个问题有一段时间了,只是不知道如何让应用程序工作并设法找到文件。我试着设置
static_url_path
在实例化flask应用程序时的值,但是,尽管这样可以找到HTML文件,但我无法加载CSS和图像等其他静态文件,因为我在HTML中定义了相对于静态文件夹的路径,如下所示:
<link href="css/bootstrap.min.css?version=24" >
或
<link rel="icon" href="/static/images/gear.png">