gpt4 book ai didi

python - Django 管理员 : image saved but error occured when click

转载 作者:行者123 更新时间:2023-11-30 22:53:59 26 4
gpt4 key购买 nike

我羞于问这样的问题,但我仍然无法解决我的问题。我通常将图像上传到我的媒体目录中,并且可以在我的管理员中看到图像链接,但如果我单击链接,我会得到:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/photo/img/9.jpg Using the URLconf defined in TeamStudy.urls, Django tried these URL patterns, in this order: ^admin/ The current URL, photo/img/9.jpg, didn't match any of these. my project structure:

src
static/
photo/
img/

设置:

PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)

MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'photo')
MEDIA_URL = '/photo/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'

url.py

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]

我怀疑这可能很简单,但我错过了一些东西。请指点我。

最佳答案

您需要在urlpatterns中添加媒体MEDIA_ROOTMEDIA_URL

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

查看以下链接了解更多详细信息

Accessing "Media" files in Django

Django 将用户文件分为两种类型

  1. 静态文件
  2. 媒体文件

this link帮助您了解它们之间的区别

您的问题涉及媒体文件。

In future development, you may need to serve static files, to serve them you will need to add STATIC_ROOT and STATIC_URL to the urlpatterns in a similar way that MEDIA_ROOT and MEDIA_URL are added

关于python - Django 管理员 : image saved but error occured when click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com