Location of templates and static files in Django

I am planning a new Django project and want to get everything right and stuff. I came across a question about how to organize the layout of a project directory. Fortunately, there are many examples of good project templates on the Internet. However, there is one thing I'm struggling to get into my head:

This is the recommended way to put the template files in a separate directory under the root of the project, which is divided into subdirectories by applications. Therefore, templates are not located in application directories. This seems logical to me, because we want to separate the application logic from the presentation logic. But what about static files? Here, it seems to be a common practice to search for static files in dir applications and load them into a “static” directory under the project root during development (collectstatic). And I don’t understand this logic. Since static files (e.g. js, css, images) are usually available in templates and not in application code, I would consider them presentation logic. Then why aren’t they stored in the same way as templates - is it a directory under the project root, with subdirectories for individual applications?

I know that I can store these files wherever I want, but I think that there may be a good reason why people do this. What is the reason?

+6
source share
2 answers

Static files can be placed in the corresponding application in the same way that templates associated with a particular application are often placed in the application directory.

Sometimes it makes sense. Sometimes it’s not - it’s your call.

For example, I put static media in the site_media directory (global css, global images, etc.), but I placed certain applications in app/static . For example, if I have a Poll application, there is a good chance that my media is needed only for polling application templates, and not for my site index.

The same goes for templates: I put my global templates (base.html) in the global template directory, but specific application templates are in myapp/templates/myapp/foo.html .

Finally, it is especially useful for plug-in applications. For example, django static files are stored in the application, but become available in your static files directory, even if the application lives somewhere in your python path. Previously, you had to copy a media catalog or a symbolic link to it.

The staticfiles application really shines because it allows you to organize all the files associated with the application in one place: the application folder. collectstatic takes care of everything else and makes everything available in one place for the web server.

+8
source

I actually put the templates in a directory for each application, and I think that makes sense. You still have logical separation, as everything else happens in Python files.

However, placing the template in the application means that if it turns out that the application is useful for other projects, you can simply copy the application directly to the new project. And you can always override these patterns by creating alternate ones in the root patterns folder.

For this reason, the static directory must also be stored in the application directory; it allows you to very clearly organize resources based on what is needed for a particular application and why the staticfiles application was created in the first place.

+5
source

Source: https://habr.com/ru/post/907530/


All Articles