代码之家  ›  专栏  ›  技术社区  ›  noli

将Django Rest Framework应用程序部署到Heroku时出现问题

  •  0
  • noli  · 技术社区  · 3 年前

    我试图将一个简单的应用程序部署到Heroku,最近开始出现以下错误:

    remote: -----> $ python manage.py collectstatic --noinput
    remote:        Post-processing 'drf-yasg/swagger-ui-dist/swagger-ui-es-bundle.js' failed!
    remote:        Traceback (most recent call last):
    remote:          File "/tmp/build_33d50075/manage.py", line 22, in <module>
    remote:            main()
    remote:          File "/tmp/build_33d50075/manage.py", line 18, in main
    remote:            execute_from_command_line(sys.argv)
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
    remote:            utility.execute()
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute
    remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 373, in run_from_argv
    remote:            self.execute(*args, **cmd_options)
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 417, in execute
    remote:            output = self.handle(*args, **options)
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
    remote:            collected = self.collect()
    remote:          File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 134, in collect
    remote:            raise processed
    remote:        whitenoise.storage.MissingFileError: The file 'drf-yasg/swagger-ui-dist/swagger-ui-es-bundle.js.map' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f4a4526efb0>.
    remote:        The JS file 'drf-yasg/swagger-ui-dist/swagger-ui-es-bundle.js' references a file which could not be found:
    remote:          drf-yasg/swagger-ui-dist/swagger-ui-es-bundle.js.map
    remote:        Please check the URL references in this JS file, particularly any
    remote:        relative paths which might be pointing to the wrong location.
    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 boundless-ea.
    remote:
    

    我不记得做出了导致这个错误的更改。我正在尝试从Heroku上的Django应用程序动态生成API文档。

    它说我正在使用这个静态文件存储后端 whitenoise.storage.CompressedManifestStaticFilesStorage 但我在代码中设置的实际上是这个: STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

    我如何修复此问题以部署我的应用程序?

    当我在本地运行collectstatic时,它工作得很好 我使用的是Django 4.0,白化5.3.0。这是我的设置文件:

    """
    Django settings for config project.
    
    Generated by 'django-admin startproject' using Django 4.0.1.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/4.0/topics/settings/
    
    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/4.0/ref/settings/
    """
    
    import environ
    from pathlib import Path
    from typing import List
    
    # Build paths inside the project like this: BASE_DIR / 'subdir'.
    BASE_DIR = Path(__file__).resolve().parent.parent
    
    env = environ.Env(
        DEBUG=(bool, False),
        HEROKU=(bool, False)
    
    )
    
    environ.Env.read_env(BASE_DIR.joinpath('.env'))
    
    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
    
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = env('SECRET_KEY')
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = env('DEBUG')
    
    ALLOWED_HOSTS = [
        'localhost',
        '127.0.0.1',
        '.herokuapp.com',
    ]
    
    # Superuser
    DJANGO_SUPERUSER_USERNAME = env('DJANGO_SUPERUSER_USERNAME')
    DJANGO_SUPERUSER_PASSWORD = env('DJANGO_SUPERUSER_PASSWORD')
    DJANGO_SUPERUSER_EMAIL = env('DJANGO_SUPERUSER_EMAIL')
    
    # Application definition
    
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'whitenoise',
        'rest_framework',
        'drf_yasg',
        'django_filters',
        'django_extensions',
        'corsheaders',
    
        'api',
        'enterprise_agreement',
    ]
    
    MIDDLEWARE = [
        'corsheaders.middleware.CorsMiddleware',
        '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',
    ]
    
    ROOT_URLCONF = 'config.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 = 'config.wsgi.application'
    
    
    # Database
    # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
    
    DATABASES = {
        'default': env.db()
    }
    
    
    # Password validation
    # https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'UTC'
    
    USE_I18N = True
    
    USE_TZ = True
    
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/4.0/howto/static-files/
    
    STATICFILES_DIRS = [
        str(BASE_DIR.joinpath('static')),
    ]
    STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
    STATIC_URL = 'static/'
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
    
    # Default primary key field type
    # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
    
    DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
    
    
    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
        'PAGE_SIZE': 100,
    
        # 'DEFAULT_RENDERER_CLASSES': (
        #     'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
        #     'djangorestframework_camel_case.render.CamelCaseBrowsableAPIRenderer',
        #     # Any other renders
        # ),
    
        # 'DEFAULT_PARSER_CLASSES': (
        #     # If you use MultiPartFormParser or FormParser, we also have a camel case version
        #     'djangorestframework_camel_case.parser.CamelCaseFormParser',
        #     'djangorestframework_camel_case.parser.CamelCaseMultiPartParser',
        #     'djangorestframework_camel_case.parser.CamelCaseJSONParser',
        #     # Any other parsers
        # ),
    
        'DEFAULT_FILTER_BACKENDS': (
            'django_filters.rest_framework.DjangoFilterBackend',
        ),
    }
    
    
    CORS_ALLOWED_ORIGINS = [
        "https://dashboard.boundlessdigital.com",
        "http://localhost:8080",
        "http://127.0.0.1:9000",
    ]
    
    if env('HEROKU'):
        import django_heroku
        django_heroku.settings(locals())
    
    
    0 回复  |  直到 3 年前