私はPythonを初めて使用しますが、すでにこの問題に直面しています。私の解決策が正しいかどうかはわかりませんが、機能します:
まず、ユーザーごとにモジュールを作成する必要があります
/flaskapp
/application.py
/templates (default template goes here)
__init__.py # default module flaskapp
views.py # here you can define methods for default module (like Action in MVC)
/hello.html
/static
/userdata
/user1
__init__.py # user1 module
views.py # here you can define methods for user1 module
/template1
hello.html
/template2
hello.html
/user2
__init__.py # user2 module
views.py # here you can define methods for user2 module
/template1
hello.html
/template2
hello.html
application.py init Flaskアプリで、グローバルメソッドrender_page_fromを追加し、ブループリントを登録します
app = Flask(__name__)
def render_page_from(controller_name, template_name_or_list, **context):
# here you can choose any controller or use default
app.jinja_loader.searchpath.clear()
blueprint = app.blueprints[controller_name]
app.jinja_loader.searchpath.append(blueprint.template_folder)
return render_template(template_name_or_list, context=context)
from flaskapp.user1 import controller as user1_controller
from flaskapp.user2 import controller as user2_controller
app.register_blueprint(user1_controller)
app.register_blueprint(user2_controller)
各モジュール(user1、user2など)のinit.pyのinitブループリント
templates_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
controller = Blueprint('user1', __name__, url_prefix='/user1', template_folder = templates_folder)
import flaskapp.user1.views
最後に、このようにviews.pyにview(アクション)メソッドを追加します
from LocalHub.client import controller
@controller.route('/hello')
def hello():
"""Renders the page"""
return render_page_from(controller.name, 'hello.html', title='hello')