What's the difference between STATIC_URL and STATIC_ROOT in Django?
Asked Answered
P

3

30

I'm somewhat confused as to what the difference is between STATIC_URL and STATIC_ROOT in Django's 'staticfiles' app.

I believe I understand what the STATIC_ROOT is: it's essentially the location on the server where the staticfiles' collectstatic command will place the static files collected from your django project. The collectstatic command searches in the locations that you specify in the STATIC_FINDERS setting.

However, what exactly does the STATIC_URL do? What should this be set to? Apparently it's intended to be set something such that users can access static files. But what is it's relationship with STATIC_ROOT?

Why is the default value of STATIC_URL simply /static/ ? Does STATIC_URL have to be able to reference STATIC_ROOT?

Peseta answered 9/6, 2016 at 3:16 Comment(0)
C
39

Like you mentioned, it is pretty clear from the documentation:

STATIC_ROOT:

The absolute path to the directory where collectstatic will collect static files for deployment.

STATIC_URL

default: None

URL to use when referring to static files located in STATIC_ROOT.

Example: "/static/" or "http://static.example.com/"

While, the STATIC_ROOT is just the path to the directory where static files have been collected, STATIC_URL is the URL which will serve those static files.

And, as you can see in the example, you can define STATIC_URL as a subdomain "http://static.example.com/" and when you use it in the template:

<link rel="stylesheet" href="{{ STATIC_URL }}css/base.css" type="text/css" />

It will be treated as:

<link rel="stylesheet" href="http://static.example.com/css/base.css" type="text/css" />

But, if the STATIC_URL was just /static/ then the above link would point to:

<link rel="stylesheet" href="/static/css/base.css" type="text/css" />

And, since this href starts with / it will append your domain to access the static files: http://yourdomain/static/css/base/css


Why is the default value of STATIC_URL simply /static/ ? Does STATIC_URL have to be able to reference STATIC_ROOT?

Default value of STATIC_URL is not /static/ but None as you can see in the documentation. And, it doesn't have to reference to STATIC_ROOT because it is not dependent on it (as shown in the example above).

Constancy answered 9/6, 2016 at 4:47 Comment(4)
Great answer. I have to add, that the use of {{STATIC_URL}} inside a template is not recommended. Use {% load static %} ... {% static 'css/main.css' %} instead. staticfiles.productiondjango.com/blog/…Snowcap
But as I just tried and found out that I can set STATIC_URL = '/static/abcd/', basically just an arbitrary path and Django still correctly fetches static files. For example, it is serving a CSS file from <link rel="stylesheet" href="/static/abcd/css/app.css" type="text/css"> as seen in the DevTools although there is no such directory on my system. My CSS file is in common_static/css/app.css and is collected to static/css/app.css. So this threw a really big wrench into my understanding of how STATIC_URL works. How is this possible?Photocompose
@Photocompose I have the same doubt. Have you found the answer to it?Parton
@Parton No, not yet, unfortunatelyPhotocompose
A
7

STATIC_URL is simply the prefix or url that is prepended to your static files and is used by the static method in Django templates mostly. For more info, read this.

STATIC_ROOT is the directory or location where your static files are deployed when you run collectstatic.

So, when you have STATIC_URL defined as /static/, then your users would request static files from /static/file-name.example (a relative URL on your server).

If you had customized your collectstatic to deploy static files to another server, you could set STATIC_URL to https://static.example.org/.

Then, you'd access your files at https://static.example.org/filename.ext.

Another example I have is using the Boto S3 library to upload static and media content to Amazon S3. My STATIC_URL looks like this:

STATIC_URL = '//%s/%s/' % (CLOUDFRONT_DOMAIN, STATIC_S3_PATH)

It constructs a static URL prefix like this //mycloudfront.whatever/static/ so users will be served files from our CDN.

My STATIC_ROOT however is defined as:

STATIC_ROOT = '/%s/' % STATIC_S3_PATH

...because I need to upload my content to Amazon S3 and not Cloudfront.

Abstemious answered 9/6, 2016 at 4:56 Comment(0)
F
4

STATIC_ROOT is where all your assets will be collected by the collectstatic command. The contents of this directly contain all static assets from all applications listed in INSTALLED_APPS (from their own static folders) and any file locations mentioned in STATICFILE_DIRS.

Once you have collected all these assets, in order for django to create urls you need to tell it what is the base URL to these assets, that's the STATIC_URL setting, and it must always end in a slash.

Fusion answered 9/6, 2016 at 4:58 Comment(1)
Would you mind looking at #37716700 ?Photocompose

© 2022 - 2024 — McMap. All rights reserved.