R
R
Ruslan Gilfanov2017-09-03 05:11:22
Django
Ruslan Gilfanov, 2017-09-03 05:11:22

Does it make sense to combine templates and static directories to implement a component approach in the front-end of Django and Flask projects?

The file structure recommended in the Flask and Django documentation separates HTML templates from their associated static files (CSS, JavaScript, and image files) into different directories.
This separation simplifies the HTTP server configuration:

  • static files must be served by the HTTP server directly;
  • HTML templates are processed by a Python application, the result goes through the WSGI server to the http server.

In Django, to simplify the distribution of statistics out of the box, the collectstatic command is available, which collects all static files in one directory.
So, for distributing statics through the same NGINX, you can specify one specific path:
location /static/ {
        alias /home/project/project-env/project/static/;
    }

But, in development, it seems advantageous to combine static and templates into one directory to keep related HTML, CSS and JS files as close as possible, implementing a hierarchy of more or less independent components (blocks in the BEM methodology).
Organizing a complete component hierarchy is easier with the Jinja2 templating engine. Compared to the Django templating engine, Jinja2 has more mechanisms for extending and including templates, isolation of namespaces and context.
At the same time, as I understand it, NGINX can directly distribute files with a specific extension (*.js, *.css, and so on). This means that the multi-level component file structure can be left in production.
However, I couldn't find any information about the advantages and disadvantages of combining static and templates directories.
Actually the question is, what are the advantages and disadvantages in Django / Flask:
  1. classic approach with frontend division into static and templates
  2. and a BEM-like approach with the division of the front-end into more independent components, more or less standard for Django/Flask means?

PS It's not about dividing the entire project into applications/blueprints, but about the front-end structure inside these applications/blueprints.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Anzhiganov, 2017-11-08
@vanzhiganov

In this case, it is better to use the Flask-Themes library to specify the location of the static and templates outside the module without polluting it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question