无法在Django中正确指定STATICFILES_DIRS

fv2wmkja  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(99)

我得到了警告:(staticfiles.W004)STATICFILES_DIRS设置中的目录“C:/frontend/build”不存在。似乎我不能正确引用我的react build文件夹,这一切都发生在尝试移动django API文件夹之后。下面是我的文件夹的样子:
enter image description here
下面是我的路径相关设置在www.example.com中的样子settings.py

import os
from pathlib import Path

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

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "frontend/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",
            ],
        },
    },
]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = "static/"

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

STATICFILES_DIRS = [
    os.path.join(
        BASE_DIR, "/frontend/build")
]

我试着调试我的settings.py,似乎BASE_DIR从未正确设置,我无法理解os.path.join

lf3rwulv

lf3rwulv1#

我被砍伤了。我解决了它修改我的模板像这样:

TEMPLATES = [
{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "DIRS": [os.path.join(BASE_DIR, "frontend/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",
        ],
    },
},

]
我的静态文件如下:

STATICFILES_DIRS = [
os.path.join(
    BASE_DIR, "frontend/build/static")
]

相关问题