python - 调试 = 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)
解决方案
推荐阅读
- firebase - cordova-plugin-firebase-config 编辑 config.xml 以指定 FirebaseRemoteConfigDefaults
- swift - 链式网络请求 RXSwift
- kotlin - Ktor websocket 客户端连接了多个服务器
- python - 在 Python 中回测库
- deployment - 如何防止在使用 Strapi 构建 Nuxt 时出错
- python - AttributeError:“敌人”对象没有属性“blit”
- python-3.x - 最简单的python链接缩短器
- browser - Loadrunner 中的 Web_set_user 不工作且浏览器未配置为使用 SAPNego
- tomcat8 - 端口 8080 上的 Tomcat SSL
- jquery - 我有一个跟随光标的 div。当我滚动时,我怎样才能让它做同样的事情?