I'm trying to tướng deploy my Django application to tướng the trang web, but I get the following error:

You're using the staticfiles ứng dụng without having mix the STATIC_ROOT setting to tướng 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 tướng find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

# Simplified static tệp tin serving.
# https://warehouse.python.org/project/whitenoise/

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Bip Lob

7232 gold badges10 silver badges18 bronze badges

asked Apr 21, năm 2016 at 5:39

If you are using Django 2.2 or greater, your settings tệp tin already has a line similar to tướng this:

# Build paths inside the project lượt thích this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Therefore you can easily mix static lượt thích so:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Kaan

5,7343 gold badges22 silver badges45 bronze badges

answered Oct 15, 2019 at 23:17

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

Depending on how you got this error (serving django through a wsgi server or on the command line), kiểm tra for manage.py or wsgi.py to tướng see what is the name of the mặc định settings tệp tin.

If you want to tướng manuallly mix the settings to tướng use, use something lượt thích this:

./manage.py --settings=production

Where production is any python module.

Moreover, your settings tệp tin should not import anything django related. If you want to tướng split your settings for different environments, use something lượt thích this.

A tệp tin settings/base.py

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

Files lượt thích settings/local.py, settings/production.py

# Production settings
from settings.base import *
DEBUG = False
DATABASES = …

answered Apr 21, năm 2016 at 6:12

Set the STATIC_ROOT setting to tướng the directory from which you’d lượt thích to tướng 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

answered Apr 21, năm 2016 at 6:28

2

Django settings for static assets can be a bit difficult to tướng configure and debug. However, if you just add the following settings to tướng your settings.py, everything should work exactly as expected:

goto "settings.py" add following code

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to tướng find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

See a full version of our example settings.py on GitHub.

now create static thư mục in root directory, and a random tệp tin inside it.

Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to tướng create this directory in your codebase, so sánh it will be available when collectstatic is lập cập. Git does not tư vấn empty tệp tin directories, so sánh you will have to tướng create a tệp tin inside that directory as well.

for more refer: https://devcenter.heroku.com/articles/django-assets

answered Jun 27, 2021 at 21:31

This is the most basic and easy solution, i faced the same error while installing datta admin. This solved my problem. Add this code in your settings.py tệp tin in your project core directory.

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / STATIC_URL

answered Aug 1, 2023 at 7:52