Django admin:图像已保存,但单击时发生错误 [英] Django admin: image saved but error occured when click
问题描述
我很ham愧地提出这样的问题,但我仍然无法解决我的问题.我通常将图像上传到我的媒体目录中,并且可以在我的管理员中看到图像链接,但是如果单击链接,我将得到:
I am ashamed to ask a question of that sort but I still can not solve my problem. I normally uploaded image in my media directory and can see image link in my admin but if I click on link I get:
找不到页面(404)请求方法:GET请求网址: http://127.0.0.1:8000/photo/img/9.jpg Django使用TeamStudy.urls中定义的URLconf,按以下顺序尝试了以下URL模式:^ admin/当前URL,photo/img/9.jpg,与这些都不匹配.我的项目结构:
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/'
urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
我怀疑这可能很简单,我想念一些东西.请指出我.
I suspect it perhaps quite simple and I miss something. Please point me.
推荐答案
您需要在 urlpatterns 中添加媒体 MEDIA_ROOT 和 MEDIA_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
查看以下链接以获取更多详细信息
check following links for more details
Django将用户文件分为两种类型
Django classifies user files in two types
- 静态文件
- 媒体文件
此链接帮助您了解它们之间的区别
this link helps you understand the difference between them
您的问题涉及媒体文件.
Your issues deals with Media files.
在将来的开发中,您可能需要提供静态文件,要提供静态文件,您需要添加 STATIC_ROOT 和 STATIC_URL 到urlpatterns的方式类似于添加 MEDIA_ROOT 和 MEDIA_URL 的方式
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
这篇关于Django admin:图像已保存,但单击时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!