Python Django:您正在使用 staticfiles 应用程序,而没有设置 STATIC_ROOT 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36760549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Django: You're using the staticfiles app without having set the STATIC_ROOT setting
提问by Henry Zhu
I'm trying to deploy my Django application to the web, but I get the following error:
我正在尝试将我的 Django 应用程序部署到 Web,但出现以下错误:
You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path
您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径
However, I did in my production.py:
但是,我在我的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'
回答by Thibault J
What is the production.py
file? How do you import your settings?
production.py
文件是什么?你如何导入你的设置?
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.
根据您收到此错误的方式(通过 wsgi 服务器或在命令行上提供 django),检查manage.py
或wsgi.py
查看默认设置文件的名称。
If you want to manuallly set the settings to use, use something like this:
如果要手动设置要使用的设置,请使用以下内容:
./manage.py --settings=production
Where production
is any python module.
production
任何python模块在哪里。
Moreover, your settings file should not import anything django related. If you want to split your settings for different environments, use something like this.
此外,您的设置文件不应导入任何与 django 相关的内容。如果你想为不同的环境拆分你的设置,使用这样的东西。
A file settings/base.py
一份文件 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
…
文件如settings/local.py
,settings/production.py
...
# Production settings
from settings.base import *
DEBUG = False
DATABASES = …
回答by Arthur Nangai
If you are using Django 2.2
or greater, your settings file already has a line similar to this:
如果您正在使用Django 2.2
或更高版本,您的设置文件已经有类似这样的一行:
# 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')
回答by Phillis Peters
Set the STATIC_ROOT setting to the directory from which you'd like to serve these files, for example:
将 STATIC_ROOT 设置设置为您想要提供这些文件的目录,例如:
STATIC_ROOT = "/var/www/example.com/static/"
STATIC_ROOT = "/var/www/example.com/static/"
The settings you are using are for development. Check the Django docs for more information here
您正在使用的设置用于开发。在此处查看 Django 文档以获取更多信息