将Django React应用程序部署到Heroku:迁移错误?:( staticfiles.w004)目录'/app/static'在staticfiles_dirs设置中不存在

发布于 2025-02-06 22:53:02 字数 4582 浏览 0 评论 0 原文

我部署的网站现在只是显示了我的CSS或数据的Django Rest框架页面(

我认为这与迁移有关,因为在运行 Heroku Run Python Manage.py Migrate i之后获取以下错误:

WARNINGS:
?: (staticfiles.W004) The directory '/app/static' in the STATICFILES_DIRS setting does not exist.

是我的设置

from pathlib import Path

import dotenv
import dj_database_url
import os

from decouple import config


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
    dotenv.load_dotenv(dotenv_file)


SECRET_KEY = config('SECRET_KEY')


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


ALLOWED_HOSTS = ['localhost', '127.0.0.1:8000', 'ever-actor.herokuapp', ]


CSRF_TRUSTED_ORIGINS = ['http://localhost:8000']

CORS_ORIGIN_WHITELIST = [
    'http://localhost:8000',
]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',

    'api.apps.ApiConfig',

    'rest_framework',

    'corsheaders',

]

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


]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

ROOT_URLCONF = 'ever_actor.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # BASE_DIR / 'ever-react/build'
            os.path.join(BASE_DIR, 'build'),
        ],
        '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 = 'ever_actor.wsgi.application'


# Database


DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)


# 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_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

CORS_ALLOW_ALL_ORIGINS = True

import django_heroku
django_heroku.settings(locals())
#https://github.com/jazzband/dj-database-url/issues/107
del DATABASES['default']['OPTIONS']['sslmode']

Project
|
+--api
|.  |
|.  +--migrations
|.  |.          |
|.  |.          +__init__.py
|.  |.          +0001_initial.py
|.  |
|.  +--__init__.py
|.  +--admin.py
|.  +--apps.py
|.  +--models.py
|   +--serializers.py
|.  +--tests.py
|.  +--urls.py
|.  +--views.py
|
+--ever-actor
|.          |
|.          +--__init__.py
|.          +--asgi.py
|.          +--settings.py
|           +--urls.py
|           +--wsgi.py
|
+--public (contains index.html)
+--src (contains react components. works on local host)
+--static
+--staticfiles
+--.env
+--db.sqlite3
+--manage.py
+--package-loc.json
+--package.json
+--Pipfile.lock
+--Procfile
+--requirements.txt 

这 我真的不知道该如何做到这一点,而不会破坏任何东西(例如,我有两个URL.PY)。还是我的db.sqlite3需要在其他地方?我设置了我的数据库= sqlite:///db.sqlite3,并在Heroku上使用postgressql。

My deployed website right now just shows the Django Rest Framework Page with none of my css or data (https://ever-actor.herokuapp.com/api/lines/)

I'm thinking it has something to do with the migrations because after running heroku run python manage.py migrate I get the following error:

WARNINGS:
?: (staticfiles.W004) The directory '/app/static' in the STATICFILES_DIRS setting does not exist.

Here is my settings.py:

from pathlib import Path

import dotenv
import dj_database_url
import os

from decouple import config


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
    dotenv.load_dotenv(dotenv_file)


SECRET_KEY = config('SECRET_KEY')


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


ALLOWED_HOSTS = ['localhost', '127.0.0.1:8000', 'ever-actor.herokuapp', ]


CSRF_TRUSTED_ORIGINS = ['http://localhost:8000']

CORS_ORIGIN_WHITELIST = [
    'http://localhost:8000',
]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',

    'api.apps.ApiConfig',

    'rest_framework',

    'corsheaders',

]

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


]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

ROOT_URLCONF = 'ever_actor.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # BASE_DIR / 'ever-react/build'
            os.path.join(BASE_DIR, 'build'),
        ],
        '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 = 'ever_actor.wsgi.application'


# Database


DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)


# 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_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

CORS_ALLOW_ALL_ORIGINS = True

import django_heroku
django_heroku.settings(locals())
#https://github.com/jazzband/dj-database-url/issues/107
del DATABASES['default']['OPTIONS']['sslmode']

I think I have my folder structure set up wrong:

Project
|
+--api
|.  |
|.  +--migrations
|.  |.          |
|.  |.          +__init__.py
|.  |.          +0001_initial.py
|.  |
|.  +--__init__.py
|.  +--admin.py
|.  +--apps.py
|.  +--models.py
|   +--serializers.py
|.  +--tests.py
|.  +--urls.py
|.  +--views.py
|
+--ever-actor
|.          |
|.          +--__init__.py
|.          +--asgi.py
|.          +--settings.py
|           +--urls.py
|           +--wsgi.py
|
+--public (contains index.html)
+--src (contains react components. works on local host)
+--static
+--staticfiles
+--.env
+--db.sqlite3
+--manage.py
+--package-loc.json
+--package.json
+--Pipfile.lock
+--Procfile
+--requirements.txt 

Do I need to put everything in the root project folder for heroku to read it? I don't really know how to do that without breaking something (for instance I have two urls.py). Or does my db.sqlite3 need to be somewhere else? I set my DATABASE_URL=sqlite:///db.sqlite3 and am using PostgresSQL on heroku.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-02-13 22:53:04

实际上是在告诉您在项目中制作一个名为static的目录

it's literally telling you to make a directory called static in your project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文