Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django: You're using the staticfiles app without having set the STATIC_ROOT setting

Tags:

python

django

I'm trying to deploy my Django application to the web, but I get the following error:

You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path

However, I did in my production.py:

from django.conf import settings

DEBUG = False
TEMPLATE_DEBUG = True
DATABASES = settings.DATABASES

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
like image 614
Henry Zhu Avatar asked Apr 21 '16 05:04

Henry Zhu


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

Which of these variables are the settings for Django contrib Staticfiles app?

Explanation : All of the above are variables are the settings for django. contib. staticfiles app.

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

What is the production.py file? How do you import your settings?

Depending on how you got this error (serving django through a wsgi server or on the command line), check for manage.py or wsgi.py to see what is the name of the default settings file.

If you want to manuallly set the settings to use, use something like this:

./manage.py --settings=production

Where production is any python module.

Moreover, your settings file should not import anything django related. If you want to split your settings for different environments, use something like this.

A file settings/base.py

# All settings common to all environments
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

Files like settings/local.py, settings/production.py

# Production settings
from settings.base import *
DEBUG = False
DATABASES = …
like image 153
Thibault J Avatar answered Oct 13 '22 00:10

Thibault J


If you are using Django 2.2 or greater, your settings file already has a line similar to this:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Therefore you can easily set static like so:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
like image 36
Arthur Nangai Avatar answered Oct 13 '22 00:10

Arthur Nangai


Set the STATIC_ROOT setting to the directory from which you’d like to serve static files, for example:

STATIC_ROOT = "/var/www/example.com/static/"

The settings you are using are for development. Check the Django docs for more information here

like image 2
Phillis Peters Avatar answered Oct 13 '22 00:10

Phillis Peters