Each Answer to this Q is separated by one/two green lines.
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:
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
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:
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.
# 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')
# Production settings from settings.base import * DEBUG = False DATABASES = …
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')
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
Django settings for static assets can be a bit difficult to configure and debug. However, if you just add the following settings to your settings.py, everything should work exactly as expected:
goto “settings.py” add following code
See a full version of our example settings.py on GitHub.
now create static folder in root directory, and a random file inside
Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.
for more refer: https://devcenter.heroku.com/articles/django-assets