在命令行中运行以下命令时,我收到此 collectstatic 错误:
C:\Users\Steven\site1>heroku run python manage.py collectstatic
Running python manage.py collectstatic on my-compository... up, run.9943 (Free)
You have requested to collect static files at the destination
location as specified in your settings:
/app/mynotes/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 115, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 399, in listdir
for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/app/mynotes/static'
经过多次搜索后,我还没有弄清楚出了什么问题。 这是我的目录结构的一个小预览:
-site1
- mynotes
- settings.py
- staticfiles (empty)
- wsgi.py
- urls.py
- mynotess
- static
- mynotess
- capture1.jpg
- carousel.css
- capture2.jpg
- templates
- views.py
- other files related to the app
- users
- files related to this app, no static files here
- manage.py
- .gitignore
- requirements.txt
- Procfile
settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False
ALLOWED_HOSTS = ['localhost']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'mynotess',
'users',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mynotes.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'mynotes/templates')], #originally empty
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mynotes.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
LOGIN_URL = 'users/login'
BOOTSTRAP3 = {'include_jquery': True}
if os.getcwd() == '/app':
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default='postgres://localhost')
}
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'static'))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
我不明白为什么会发出此错误。它也不适用于python manage.py collectstatic
,这给我一个错误如下:django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
我已经指定了静态根 mynotes / static 它已被设置为收集那里的所有静态文件。我不明白为什么它告诉我没有这样的目录。令我困惑的另一件事就是这一行:
/app/mynotes/staticfiles/
我没有名为 staticfiles 的目录,所以我不知道这是怎么来的。我在STATIC_ROOT中键入了 static ,因此我不确定它为什么会出现在 staticfiles 中。这是值得担心的事情还是这个默认的事情?可能是因为我上次部署它时更改了代码(这是DEBUG=True
时),但它还没有更新?我尝试制作一个名为 staticfiles 的空目录,但它仍然给我同样的错误。
我是Django的新手,非常感谢任何帮助!附:我已经彻底阅读了文档,但我没有得到任何有用的东西。
修改
我得到 heroku运行python manage.py collectstatic 来工作。我弄清楚为什么 staticfiles 在那里(只需要重新部署它)。但是,我的JPG / PNG图像和CSS在部署后仍然没有出现在我的网站上。这仅适用于DEBUG = False
(它在True
时有效)。另外, python manage.py collectstatic 仍然会出现同样的错误,但我不确定它是否重要?
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.views.static import serve
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls', namespace='users')),
url(r'', include('mynotess.urls', namespace= "mynotess")), #telling it to look into the urls.py in mynotess for more URLS!
]
if not settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', serve, {
'document_root': settings.STATIC_ROOT,
}),
]