Explore Flask

8.2. How to organize templates

So where do templates fit into our app? If you've been following along at home, you may have noticed that Flask is really flexible about where we put things. Templates are no exception. You may also notice that there's usually a recommended place to put things. Two points for you. For templates, that place is in the package directory.

myapp/
    __init__.py
    models.py
    views/
    templates/
    static/
run.py
requirements.txt

templates/
    layout.html
    index.html
    about.html
    profile/
        layout.html
        index.html
    photos.html
    admin/
        layout.html
        index.html
        analytics.html

The structure of the templates directory parallels the structure of our routes. The template for the route myapp.com/admin/analytics is templates/admin/an-alytics.html. There are also some extra templates in there that won't be rendered directly. The layout.html files are meant to be inherited by the other templates.