Я уверен, что есть и другие способы добиться этого, но один из них — использовать структуру схемы и содержать все статические файлы для этой конкретной схемы внутри папки схемы.
app
|- collections
| - blueprint_one
| - configs
| - static
| - css
| - js
| js_file_one.js
| - templates
| - blueprint_one
| html_file_one.html
| blue_print_one_view_file_one.py
| __init__.py
| - blueprint_two
| - blueprint_three
|- static
| - js
| - css
|- templates
__init__.py
Приведенная выше структура папок позволяет отделить не только статические файлы, но и файлы представления Flask.
Ниже приведены примеры активации/импорта файлов:
1) приложение/инициализация.py
app = Flask(__name__, static_folder='static')
app.config.from_pyfile('app_config.py')
# Do all your db set up and etcs..
# Import blueprints below set ups to prevent circular import
app.collections.blueprint_one import blueprint_one
app.collections.blueprint_two import blueprint_two
# Now register blueprints that you've imported
app.register_blueprint(blueprint_one, url_prefix='/blueprint_one')
app.register_blueprint(blueprint_two, url_prefix='/blueprint_two')
2) blueprint_one/init.py
# Create a blueprint
from flask import Blueprint
blueprint_one = Blueprint('blueprint_one_name', __name__, template_folder='templates', static_folder='static')
# Import required views to use
from . import blue_print_one_view_file_one.py
3) blueprint_one/blue_print_one_view_file_one.py
# Import blueprint
from app.collections.blueprint_one import blueprint_one
# Any view functions using above blueprint
@blueprint_one.route('/login/, methods=['GET'])
def login():
return render_template('blueprint_one/html_file_one.html')
4) blueprint_one/templates/blueprint_one/html_file_one.html
// Note: instead of .static, you can use blueprint_one.static.
<script type="text/javascript" src="{{ url_for('.static', filename = 'js/js_file_one.js') }}"></script>
5) приложения/статические и приложения/шаблоны по-прежнему можно использовать так, как вы используете их в настоящее время.
Это решение не решает проблему размещения js и html в одной папке, но, по крайней мере, вы можете разделить каждую функцию на компоненты, такие как план, для более модульной структуры проекта.
person
Jessi
schedule
28.06.2019
templates
в папкуstatic
. - person Avm-x   schedule 28.06.2019