Django 项目未获取静态文件

问题描述 投票:0回答:1

我正在为我的 Django 课程开发一个小应用程序。现在,我正在尝试将我的项目转移到云系统,在本例中使用 Heroku,但是在运行

collectstatic
命令时获取静态文件似乎存在问题。我收到以下错误:

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "/Users/mlopez/journal_project/manage.py", line 22, in <module>
    main()
  File "/Users/mlopez/journal_project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
                ^^^^^^^^^^^^^^
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/core/files/storage/filesystem.py", line 165, in exists
    return os.path.lexists(self.path(name))
                           ^^^^^^^^^^^^^^^
  File "/Users/mlopez/anaconda3/lib/python3.11/site-packages/django/contrib/staticfiles/storage.py", line 39, in path
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

这是我的设置.py:

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-@9*9cg2wtc+*%y920pa1-jfxunvkc8&gb#kwafbg2_rq!i&(nx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',')

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    'entries',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
    'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'journal_backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'journal_backend.wsgi.application'

# Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'xxx',
        'USER': 'xxx',
        'PASSWORD': 'xxx',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

# Password validation
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
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'entries.storage.CompressedManifestStaticFilesStorage'

print("STATIC_ROOT is set to:", STATIC_ROOT)

我的目录如下:files: root/staticfiles 和 app: root/app。

任何方向都非常感谢,谢谢!

我在 settings.py 中添加了 STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 行来指示收集这些文件的位置。 我目前没有任何静态文件,但就文档而言,我至少应该得到以下内容:

Type 'yes' to continue, or 'no' to cancel: yes

0 static files copied to '/path/to/staticfiles', 0 unmodified.
django heroku django-staticfiles
1个回答
0
投票

问题解决了!当我复制我的项目时,我有另一个正在配置的 settings.py 文件,并不断抛出错误消息。我找到了该项目引用的真实文件,并添加了

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
,现在可以使用了。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.