django static files

The more I read about django statics , the less I know…

For now I understand there are three places for static files:

  1. place of collectstatics (STATIC_ROOT with STATIC_URL in django and location '/static/ in nginx
  2. project folder of statics
  3. app folder of statics (one for each app)
/home/project/
    app1/
        static/
            admin.css (1)
    app2/
        static/
            admin.css (2)
    app3/
       static/
           noadmin.css
    static/
         admin.css (3)
    staticfiles/

I think that adding admin.css to staticfiles is bad idea - because I think we should expect that staticfiles shouldn’t be synchronised (backed up) - admin can always empty this folder and python manage.py collectstatic should fix everytime

But there’s a problem with nginx… Nginx is serving /static/ to staticfiles/ as expected, but what about all other static folders???

How should I use other statics in my templates?

I want to use admin.css in base template, so If it is used by app1 or app2 it uses admin.css from corresponding folder (1) or (2) and when used by app3 it should use admin.css from project’s admin folder (3)

Now I see that django can’t serve static files…

Didn’t Work?

That is right, the example still does not work.

You will have install a third-party library in order to handle static files.

Yes, this is true for two different situations.

During development, you place your static files in either (or both) app ‘static’ directories and your project ‘static’ directory.

Then, for deployment, you run collectstatic to copy those files (in addition to system-provided static files in their directories) to a “common location” defined by STATIC_ROOT to be accessed by your webserver (e.g., nginx).

STATIC_ROOT defines the directory to which all the static files will be copied.

STATIC_URL identifies what url to use as the “prefix” to reference those files.

If you’re using nginx as your webserver, then it’s the nginx configuration that maps the url to the physical directory.

For example, if STATIC_ROOT is /var/www/myproject/static, and STATIC_URL is 'static/', then you might have an alias directive that looks like this:

location /static/ {
    alias /var/www/myproject/static/;
}

(You may also wish to read: Django and Nginx permission issue on Ubuntu - #2 by KenWhitesell)

Side note: To avoid naming conflicts between files with the same name in different apps, you should have a directory named the same as the app within the app static directory. See How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django, especially including the box labeled “Static file namespacing”.

This is also covered in the official tutorial at Writing your first Django app, part 6 | Django documentation | Django.

This means that if those three admin.css files are different, they should actually be at:

/home/project/
    app1/
        static/
            app1/
                admin.css (1)
    app2/
        static/
            app2/
                admin.css (2)
    app3/
       static/
            app3/
                noadmin.css (1)
    static/
         admin.css (3)

Your reference to these admin.css files would then be something like {% static 'app1/admin.css' %}

This leads us to:

This is not something that the rendering engine handles (or even can handle) itself. You either need to create a block in the base template that your app template can use to define which css file to use, or a context variable, or create a context processor. But if you want a different static file to be used by an app, it’s up to you to manage that requirement.

Yet another substandard source. (You’ll find at least one other thread here where I’ve had to clarify or correct information provided there.)

Please, do yourself a favor and use sources known to be valid and current. I suggest you use GitHub - wsvincent/awesome-django: A curated list of awesome things related to Django as your starting point.

Incorrect. Or, at best, misleading and ignoring some important context around handling static files.

In a production deployment environment, you do not want Django serving static files. That’s a job for the web server.

1 Like