简体   繁体   English

找不到媒体 url 的页面 404,从数据库 Django 3.0.1 获取图像名称

[英]Page not found 404 for media url, getting images name from database Django 3.0.1

This is my project folders这是我的项目文件夹

项目文件夹

I first decided to set my media url and my media root on my settings.py我首先决定在我的 settings.py 上设置我的媒体网址和媒体根目录

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

On my admin side, I decided to give it an url pattern in local, since I tho while I am in local it wouldnt be able to get my URL "I wasnt sure at all since on the documentation nothing was talking about url pattern"在我的管理方面,我决定在本地给它一个 url 模式,因为我在本地时它无法获取我的 URL “我完全不确定,因为在文档中没有任何关于 url 模式的讨论”

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I have define my model this way.我以这种方式定义了我的模型。

image = models.ImageField(upload_to='portfoliopicture/')

And using the url to get the image,并使用 url 获取图像,

src="{{ portfolio.image.url }}"

Inside my template html, this is how i call it, This issue I am getting is so weird, It's like it isnt able to find the url path..在我的模板 html 中,我是这样称呼它的,我遇到的这个问题太奇怪了,就像它无法找到 url 路径一样..

This is the message error I am getting.这是我收到的消息错误。 coming from the network tab来自网络选项卡

I would like to understand.我想明白。 Why am I having this issue when the url seems to be defined.当 url 似乎已定义时,为什么我会遇到这个问题。 Is it an issue with the way I'm calling the folders?我调用文件夹的方式有问题吗? I am clearly missing something here.我显然在这里遗漏了一些东西。

this happen when i add the prefix manually当我手动添加前缀时会发生这种情况

 src="portfoliopicture/{{ portfolio.image.url }}"

我有类似的问题我的解决方案

<img class="img-fluid" src="myportfolio/{{ portfolio.image.url }}" alt="">
                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM