Y
Y
Your_House2019-11-09 22:04:25
Django
Your_House, 2019-11-09 22:04:25

Working with STATIC_ROOT, STATICFILES_DIRS, MEDIA_ROOT, MEDIA_DIRS. How do they work and where should the directories specified in them be placed?

Here, after a considerable amount of time, I walked through the entire office. documentation about static files, watched a video on YouTube on this topic, and now I began to understand a lot, HOWEVER. There are some misunderstandings related to STATIC_ROOT, STATICFILES_DIRS, MEDIA_ROOT, MEDIA_DIRS.
Please explain to me in a simpler way where the directories specified in these variables should be stored. Here is, I have, for example, a site with several applications. Where should I put the folder "associated with STATIC_ROOT" and where should I put the folder "associated with STATICFILES_DIRS"?
Variable STATIC_ROOT can be only one, and the directory specified in it is also only one? It's just that the documentation says that the "static" folder (associated with STATIC_ROOT) should be created in the APPLICATION directory, and not in the project root. But then it would be strange if all the files associated with a bunch of DIFFERENT applications are stored in the directory of ONE of them. Wouldn't it make more sense to create this static folder in the PROJECT directory?
Damn, it’s hard now to fully formulate what exactly I don’t understand and raises questions. So if anything, I'll add along the way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan, 2019-11-09
@DollaR84

The general static folder for the project should be at the root of the project, and the applications should have their own static folders, but there is one caveat.
First, in the settings.py file, you need to set the following, in the place where the static parameters are specified:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'staticfiles'))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

then you need to organize the following folder structure:
project/
    project/
        settings.py
        ...
    application1/
        static/
            application1/
                ... тут статические файлы для приложения 1
    application2/
        static/
            application2/
                ... тут статические файлы для приложения 2
    static/
        ...тут статические файлы для проекта
    manage.py
...

That is, static files are immediately placed in the root directory, and in the application directories, in the static folder, you need to create a folder with the name of the application and place static files in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question