Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically select template directory to be used in flask?

By default flask uses template files stored in "template" directory :

/flaskapp     /application.py     /templates         /hello.html 

Is there any way to dynamically choose template directory according to user logged in? This is how I want the directory structure to be :

/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 

Now if I have the username of logged in user and the name of template activated by user, is it possible to dynamically select the directory to load template files? For example,

/userdata/<username>/<activated template name>/ 

instead of fixed

/templates/ 

What I am trying to achieve is a wordpress like theme system for my web application where users can upload/select themes for his website.

like image 867
anujkk Avatar asked Nov 28 '12 05:11

anujkk


People also ask

Can we inherit templates in Flask?

Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.

Where are Flask templates stored?

html template file in a directory called templates inside your flask_app directory. Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

What template does Flask use?

Flask uses the Jinja template library to render templates. In your application, you will use templates to render HTML which will display in the user's browser. In Flask, Jinja is configured to autoescape any data that is rendered in HTML templates.


1 Answers

You can pass the Flask constructor a "template_folder" argument.

Like so...

Flask(__name__, template_folder="wherever") 

Here's the documentation: http://flask.pocoo.org/docs/api/

like image 70
nathan Avatar answered Sep 29 '22 01:09

nathan