【发布时间】:2021-10-24 09:56:57
【问题描述】:
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '../media/images/'
urls.py
if settings.DEBUG:
# Use static() to add url mapping to serve static files during development (only)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
profile_image = models.ImageField(upload_to="../media/images")
文件路径:
-main_project
/app #django app
/media/images #media folder inside image folder
/main #django main app
/static/css #static folder for js,css,images,fonts
/templates #html templates
我也尝试过其他方式,但我的图片没有上传到媒体文件夹中
【问题讨论】:
-
你的图片是
showing通过上传图片的url成功在Django Admin吗? -
是的 @Van 图像通过 Django Admin 成功显示
标签: python django django-models django-forms django-templates