это мой первый удар при использовании чертежей колбы. Я не уверен на 100%, что я правильно понимаю. Вот мой план:
page_bp = Blueprint('page', __name__, template_folder='templates')
@page_bp.route('/', methods=['GET'])
def index():
rule = request.url_rule
folder = str(rule).replace("/", "")
path = ('%s/index.html' % folder)
return template_at(path)
@page_bp.route('/theory', methods=['GET'])
def theory():
rule = request.url_rule
parts = str(rule).split("/")
path = ('%s/examples.html' % parts[0])
return template_at(path)
@page_bp.route('/examples', methods=['GET'])
def examples():
rule = request.url_rule
parts = str(rule).split("/")
path = ('%s/examples.html' % parts[0])
return template_at(path)
def template_at(path):
try:
return render_template(path)
except TemplateNotFound:
# TODO: log
abort(500)
Моя идея состоит в том, чтобы иметь один проект для 3 различных разделов моего веб-приложения. Каждый раздел имеет отдельную папку с шаблонами, но имена шаблонов совпадают.
При регистрации проекта я установил url_prefix
, который совпадает с именем папки, в которой находятся шаблоны этой страницы:
flask_app.register_blueprint(page_bp, url_prefix="/classification")
flask_app.register_blueprint(page_bp, url_prefix="/regression")
flask_app.register_blueprint(page_bp, url_prefix="/clustering")
Таким образом, в папке templates
есть папка classification
, папка regression
и папка clustering
, каждая с index.html, theory.html, examples.html
.
Мне не очень нравится идея разбора URI. Есть ли другой способ настроить это? Или чертежи не предназначены для такого использования?