首页 > 解决方案 > 调试 = False 时未加载 Django 媒体图像

问题描述

我不知道出了什么问题,该项目运行良好,因此我尝试将其托管在 heroku 上,但发现静态文件存在一些问题,我更改了 settings.py 中的 STATIC_URL,然后静态文件加载得很好,但是存储在数据库中的图像在网站上不可见

我的文件树

C:.
├───accounts
│   ├───migrations
│   │   └───__pycache__
│   ├───templates
│   └───__pycache__
├───blog
│   ├───archives
│   │   └───templates
│   ├───migrations
│   │   └───__pycache__
│   ├───templates
│   └───__pycache__
├───djago_project
│   └───__pycache__
├───media
│   ├───blog_covers
│   │   └───20
│   │       └───05
│   │           ├───18
│   │           └───19
│   └───images
│       └───blocks
│           └───20
│               └───05
│                   └───18
└───static
    ├───admin
    │   ├───css
    │   │   └───vendor
    │   │       └───select2
    │   ├───fonts
    │   ├───img
    │   │   └───gis
    │   └───js
    │       ├───admin
    │       └───vendor
    │           ├───jquery
    │           ├───select2
    │           │   └───i18n
    │           └───xregexp
    ├───images
    ├───scripts
    └───styles

我的 settings.py 文件

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, 'static') 
STATIC_URL = '/static/'

MEDIA_ROOT =  os.path.join(BASE_DIR, 'media') 
MEDIA_URL = '/media/'

我的 urls.py 文件

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('blog.urls')),
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

标签: pythondjangodjango-modelsdjango-staticfilesdjango-settings

解决方案


推荐阅读