Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 290
Izzo Avatar asked Jun 09 '16 03:06

Izzo


People also ask

What is STATIC_ROOT in Django?

"STATIC_ROOT" sets the absolute path to the folder where static files used for the apps and admin in a django project are stored and this command below creates the folder and collects static files from the apps and admin in a django project into the folder (*Setting "STATIC_ROOT" never ever influence to static file URL ...

What is static URL and static root in Django?

STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. The command manage.py collectstatic will automatically compile all the static files throughout the project and dump it into a single root directory, which is declared in STATIC_ROOT .

What is Staticfiles_dirs Django?

STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.

How do I run Collectstatic in Django?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.


3 Answers

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).

like image 156
AKS Avatar answered Sep 19 '22 19:09

AKS


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.

like image 37
A. J. Parr Avatar answered Sep 19 '22 19:09

A. J. Parr


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.

like image 23
Burhan Khalid Avatar answered Sep 18 '22 19:09

Burhan Khalid