[英]Display image from Django database to React JS frontend
I'm having an issue where the images I upload to Django database aren't displaying into the React JS frontend.我遇到了一个问题,我上传到 Django 数据库的图像没有显示到 React JS 前端。 I created this model:
我创建了这个 model:
class Project(models.Model):
title = models.CharField(max_length=100, null=False)
description = models.CharField(max_length=10000, null=False)
thumbnail = models.ImageField(upload_to='images/', null=False)
I'e serialized it and all that.我已经序列化了它和所有这些。 I then upload that information to the database by going to the /admin url where I can see the database.
然后我通过访问 /admin url 将这些信息上传到数据库,在那里我可以看到数据库。 After that, I make an api call to the database and fetch all the data.
之后,我对数据库进行 api 调用并获取所有数据。 Now this is where the issue occurs.
现在这就是问题发生的地方。 I move that data into a useState([]) and then map that state.
我将该数据移动到 useState([]) 中,然后将 map 移动到 state 中。 The title and description do get displayed on the screen, but the image doesn't.
标题和描述确实会显示在屏幕上,但图像不会。 Here is the code where I map the useState([]):
这是我 map 使用状态([])的代码:
{projData.map(data => (
<div key={data.id}>
<img className="thumbnail" src={data.thumbnail}/>
<span className="title">{data.title}</span>
<p className="description">{data.description}</p>
</div>
))}
So like I said, the title and description do successfully get called, but this is the error I get for the image:所以就像我说的,标题和描述确实被成功调用,但这是我得到的图像错误:
[Error] Failed to load resource: http://127.0.0.1:8000/images/background.png the server responded with a status of 404 (Not Found) (background.png, line 0) [错误] 加载资源失败: http://127.0.0.1:8000/images/background.png服务器响应状态为 404(未找到)(background.png,第 0 行)
The image does get moved into the images folder.图像确实被移动到图像文件夹中。 But for some reason, it doesn't display into the actual website.
但由于某种原因,它不会显示在实际的网站中。 What can I do?
我能做些什么?
I think that you are passing the image, not its location.我认为您传递的是图像,而不是它的位置。
Probably you need to, make it src={data.thumbnail.url}
可能您需要将其
src={data.thumbnail.url}
you must config your media root, add this to your settings file您必须配置您的媒体根目录,将其添加到您的设置文件中
STATICFILES_DIRS=[
os.path.join(BASE_DIR,"assets")
]
STATIC_ROOT=os.path.join(BASE_DIR,"static_cdn","static_root")
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,"static_cdn","media_root")
assets folder is where you store your files locally,you can skip it cause its not related to your problem. assets 文件夹是您在本地存储文件的位置,您可以跳过它,因为它与您的问题无关。 static root gonna be files like css and java script,and media root are files like images then add below to your url file
static root gonna be files like css and java script,and media root are files like images then add below to your url file
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and dont forget to upload the image again to be stored in media root.并且不要忘记再次上传图像以存储在媒体根目录中。 now u have access to your media files in frontend, see this for more info https://docs.djangoproject.com/en/3.2/howto/static-files/
现在您可以在前端访问您的媒体文件,请参阅此了解更多信息https://docs.djangoproject.com/en/3.2/howto/static-files/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.