我目前正在开发一个Django + React Web应用程序,我遇到了一个提供静态文件的问题。Django开发服务器返回了几个静态文件和图像的404错误,例如图像“K5.png”,“earth.png”和“manifest.json”。
是的,网站加载,除了图像。
这就是我如何引用我的图像(示例):
<img src="/earth.png" alt="" className="earth w-fit mx-auto"/>
我已经配置了我的设置如下:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR_FRONTEND, 'frontend/build/'),
]
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR_BACKEND = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR_FRONTEND = os.path.dirname(BASE_DIR_BACKEND)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
CORS_ORIGIN_ALLOW_ALL = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Installed Application
'api',
'rest_framework',
'djongo',
'pymongo',
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
'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',
]
CORS_ALLOW_METHODS = (
"GET",
)
ROOT_URLCONF = 'project_k5.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR_BACKEND, '../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',
],
},
},
]
WSGI_APPLICATION = 'project_k5.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/4.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/4.2/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.2/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR_FRONTEND, 'frontend/build/'),
]
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
下面是我的控制台输出:
[19/Oct/2023 09:38:38] "GET /K5.png HTTP/1.1" 404 2317
[19/Oct/2023 09:38:38] "GET /earth.png HTTP/1.1" 404 2326
[19/Oct/2023 09:38:38] "GET /manifest.json HTTP/1.1" 404 2338
下面是运行pnpm run build后的项目结构:
PROJECT KS
├── .github
├── idea
│ └── Build
│ └── static
│ ├── asset-manifest.json
│ ├── earth.png
│ ├── favicon.ico
│ ├── index.html
│ ├── K5.png
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.ext
├── backend
└── frontend
└── node_modules
└── public
├── earth.png
├── favicon.ico
├── index.html
├── K5.png
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
我已经验证了这些静态文件存在于指定的目录中,并且我的模板中的路径被正确地设置为访问这些文件。但是,我仍然得到这些404错误。
有人能帮我诊断问题并提出解决方案吗?我将感谢任何指导,以解决这个问题,使我的静态文件加载预期。
提前感谢您的帮助!
1条答案
按热度按时间bn31dyow1#
你必须使用Django抓取静态文件的方法。而不是使用
src="/earth.png