What's The Difference Between Static_url And Static_root In Django?
Solution 1:
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 referenceSTATIC_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).
Solution 2:
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.
Solution 3:
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.
- Django 1.4 Static Files Problems And Don't Render At Other Urls Of My Project
- 'python Manage.py Collectstatic' Is Still Collecting Static To C Drive Instead Of S3 Bucket, Am I Missing Something?
- Django | Joined Path Is Located Outside Of The Base Path Component {% Static Img.thumbnail.url %}, Error 400 With Whitenoise
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.
Post a Comment for "What's The Difference Between Static_url And Static_root In Django?"