默认情况下,flask 使用存储在"template"目录中的模板文件:
/flaskapp
/application.py
/templates
/hello.html
有没有办法根据用户登录动态选择模板目录?这就是我想要的目录结构:
/flaskapp
/application.py
/templates (default template goes here)
/hello.html
/userdata
/user1
/template1
hello.html
/template2
hello.html
/user2
/template1
hello.html
/template2
hello.html
现在如果我有登录用户的用户名和用户激活的模板名称,是否可以动态选择目录来加载模板文件?例如,
/userdata/<username>/<activated template name>/
而不是固定
/templates/
我想要实现的是一个类似 wordpress 的主题系统,用于我的 Web 应用程序,用户可以在其中上传/选择他的网站的主题。
最佳答案
您可以向 Flask 构造函数传递一个“template_folder”参数。
就这样……
Flask(__name__, template_folder="wherever")
关于python - 如何动态选择要在 flask 中使用的模板目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598363/