未找到静态文件目录(heroku)

时间:2021-04-23 20:42:59

标签: python django heroku

我正在用 Django 2.2 制作一个网站。但是当我尝试运行 git push heroku master 时出现错误。我尝试了所有 stackoverflow 的答案,但没有任何效果。 这是我的目录:

├───.idea
│   └───inspectionProfiles
├───.vscode
├───CheemsCode_Site
│   └───__pycache__
└───homepage
    ├───migrations
    │   └───__pycache__
    ├───static
    │   ├───css
    │   ├───img
    │   │   ├───portfolio
    │   │   ├───team
    │   │   └───testimonials
    │   ├───js
    │   └───vendor
    │       ├───bootstrap
    │       │   ├───css
    │       │   └───js
    │       ├───bootstrap-icons
    │       │   └───fonts
    │       ├───boxicons
    │       │   ├───css
    │       │   └───fonts
    │       ├───glightbox
    │       │   ├───css
    │       │   └───js
    │       ├───isotope-layout
    │       ├───php-email-form
    │       ├───purecounter
    │       └───swiper
    ├───templates
    └───__pycache__

这是我的 settings.py:

"""


# Application definition

INSTALLED_APPS = [
    'homepage',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic',  # new!
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',  # new!
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'CheemsCode_Site.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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 = 'CheemsCode_Site.wsgi.application'


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # new!
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

这里是错误:

(CheemsCode-hO4sBLUa) (CheemsCode) E:\Projects(django)\CheemsCode\CheemsCode_Site>git push heroku master
Enumerating objects: 535, done.
Counting objects: 100% (535/535), done.
Delta compression using up to 8 threads
Compressing objects: 100% (492/492), done.
Writing objects: 100% (535/535), 8.71 MiB | 419.00 KiB/s, done.
Total 535 (delta 255), reused 132 (delta 33), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Python app detected
remote: -----> Installing python-3.9.4
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing dependencies with Pipenv 2020.11.15
remote:        Installing dependencies from Pipfile.lock (849d40)...
remote: -----> Installing SQLite3
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/tmp/build_dda2808f/manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 316, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 353, in execute   
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
remote:            for path, storage in finder.list(self.ignore_patterns):
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/finders.py", line 125, in list
remote:            for path in utils.get_files(storage, ignore_patterns):
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
remote:            directories, files = storage.listdir(location)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/files/storage.py", line 313, in listdir     
remote:            for entry in os.listdir(path):
remote:        FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_dda2808f/static'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote:
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote:
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to cheemscode.
remote:
To https://git.heroku.com/cheemscode.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/cheemscode.git'

这是我运行 python manage.py collectstatic 时的输出:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\core\management\base.py", line 364, in execute 
    output = self.handle(*args, **options)
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 188, in handle
    collected = self.collect()
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 105, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\contrib\staticfiles\finders.py", line 131, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\contrib\staticfiles\utils.py", line 23, in get_files
    directories, files = storage.listdir(location)
  File "C:\Users\Asus\.virtualenvs\CheemsCode-hO4sBLUa\lib\site-packages\django\core\files\storage.py", line 315, in listdir   
    for entry in os.scandir(path):
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'E:\\Projects(django)\\CheemsCode\\CheemsCode_Site\\static'

我该如何解决这个问题?我搜索了 google 和 SO,但没有任何效果。

1 个答案:

答案 0 :(得分:0)

好的,我通过将 STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] 更改为 STATICFILES_DIRS = [os.path.join(BASE_DIR, 'homepage/static'), ] 来解决它。

希望它会奏效