看我咯 2021-05-25 20:03 采纳率: 0%
浏览 89

将django项目部署到heroku上时出现错误 STATIC_ROOT怎么办?

我将我的django项目部署到heroku时,出现错误提示:ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
这是我的setting.py代码:

"""
Django settings for learning_log project.

Generated by 'django-admin startproject' using Django 3.2.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-56-!b=f0shatsq%bw)8%b_#ipw9n^9q6##)dg(a4^00+tzg5&d'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap3',
    'learn_logs',
    '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 = 'learning_log.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 = 'learning_log.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_URL = '/users/login/'
BOOTSTRAP3 = {
    'include_jquery': True,
}


if os.environ['HOME'] == '/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 = ('staticfiles')
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]




下面是运行git push heroku master 后的提示:
 

(ll_env) D:\pythonProject\learn_log>git push heroku master
Enumerating objects: 45, done.
Counting objects: 100% (45/45), done.
Delta compression using up to 8 threads
Compressing objects: 100% (37/37), done.
Writing objects: 100% (45/45), 11.98 KiB | 1.09 MiB/s, done.
Total 45 (delta 3), reused 0 (delta 0), 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: -----> Using Python version specified in runtime.txt
remote: -----> Installing python-3.9.5
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote:        Collecting asgiref==3.3.4
remote:          Downloading asgiref-3.3.4-py3-none-any.whl (22 kB)
remote:        Collecting dj-database-url==0.5.0
remote:          Downloading dj_database_url-0.5.0-py2.py3-none-any.whl (5.5 kB)
remote:        Collecting dj-static==0.0.6
remote:          Downloading dj-static-0.0.6.tar.gz (3.4 kB)
remote:        Collecting Django==3.2.3
remote:          Downloading Django-3.2.3-py3-none-any.whl (7.9 MB)
remote:        Collecting django-bootstrap3==15.0.0
remote:          Downloading django_bootstrap3-15.0.0-py3-none-any.whl (213 kB)
remote:        Collecting gunicorn==20.1.0
remote:          Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
remote:        Collecting pytz==2021.1
remote:          Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
remote:        Collecting sqlparse==0.4.1
remote:          Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
remote:        Collecting static3==0.7.0
remote:          Downloading static3-0.7.0.tar.gz (24 kB)
remote:        Collecting psycopg2>=2.6.1
remote:          Downloading psycopg2-2.8.6.tar.gz (383 kB)
remote:        Building wheels for collected packages: dj-static, static3, psycopg2
remote:          Building wheel for dj-static (setup.py): started
remote:          Building wheel for dj-static (setup.py): finished with status 'done'
remote:          Created wheel for dj-static: filename=dj_static-0.0.6-py3-none-any.whl size=3053 sha256=28eb16a1cf40c479f43b0b863a0db4cd8e0428c190c33afddc1b5d2a86aed38c
remote:          Stored in directory: /tmp/pip-ephem-wheel-cache-002etsp9/wheels/b5/10/4a/83f0c20117a4580af66c382f5cd1c689e6a8818a042ffdedd3
remote:          Building wheel for static3 (setup.py): started
remote:          Building wheel for static3 (setup.py): finished with status 'done'
remote:          Created wheel for static3: filename=static3-0.7.0-py3-none-any.whl size=18605 sha256=7318f9db56f796cb5a2c8ee8749567a05729938e568e1b9010bfb1c74c3e7410
remote:          Stored in directory: /tmp/pip-ephem-wheel-cache-002etsp9/wheels/67/88/26/362b5f8314043c6241ed324b9f76ddf48328b775923b39590a
remote:          Building wheel for psycopg2 (setup.py): started
remote:          Building wheel for psycopg2 (setup.py): finished with status 'done'
remote:          Created wheel for psycopg2: filename=psycopg2-2.8.6-cp39-cp39-linux_x86_64.whl size=523769 sha256=a70ab92a961c3e7ae9c6a4a1249a6f6e44ae6a626e639aa5b4c68474fa
52c99d
remote:          Stored in directory: /tmp/pip-ephem-wheel-cache-002etsp9/wheels/a2/07/10/a9a82e72d50feb8d646acde6a88000bbf2ca0f82e41aea438a
remote:        Successfully built dj-static static3 psycopg2
remote:        Installing collected packages: asgiref, dj-database-url, static3, dj-static, pytz, sqlparse, Django, django-bootstrap3, gunicorn, psycopg2
remote:        Successfully installed Django-3.2.3 asgiref-3.3.4 dj-database-url-0.5.0 dj-static-0.0.6 django-bootstrap3-15.0.0 gunicorn-20.1.0 psycopg2-2.8.6 pytz-2021.1 sq
lparse-0.4.1 static3-0.7.0
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/tmp/build_db5684bd/manage.py", line 22, in <module>
remote:            main()
remote:          File "/tmp/build_db5684bd/manage.py", line 18, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, 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 354, 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 398, 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 187, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
remote:            handler(path, prefixed_path, storage)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 338, in copy_file
remote:            if not self.delete_file(path, prefixed_path, source_storage):
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 248, in delete_file
remote:            if self.storage.exists(prefixed_path):
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/files/storage.py", line 318, in exists
remote:            return os.path.exists(self.path(name))
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py", line 38, in path
remote:            raise ImproperlyConfigured("You're using the staticfiles app "
remote:        django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
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:  !
remote:  ! ## Warning - The same version of this code has already been built: 4b89831e6bfb7899d13d8104d47863fa6334a335
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 4b89831e6bfb7899d13d8104d47863fa6334a335
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote:
remote: Verifying deploy...
remote:
remote: !       Push rejected to pure-stream-85980.
remote:
To https://git.heroku.com/pure-stream-85980.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/pure-stream-85980.git'
  • 写回答

1条回答 默认 最新

  • devid008 2023-03-25 08:34
    关注

    在Heroku上部署Django应用时,您需要配置静态文件设置。您在setting.py中没有设置STATIC_ROOT,因此出现了ImproperlyConfigured错误。

    您需要在setting.py中设置STATIC_ROOT为存储您的静态文件的文件系统路径。您可以像这样将其设置为某个目录:

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    

    这将创建一个名为staticfiles的目录,以存储应用程序的静态文件。如果您在应用程序中使用了任何静态文件,例如CSS,JavaScript或图像文件,则需要将它们存储在此目录中。

    记得在完成设置后再次将代码部署到Heroku。

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛