简体   繁体   English

从 Django 数据库显示图像到 React JS 前端

[英]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.

相关问题 如何在反应前端显示来自strapi的图像? - How to display image from strapi in react frontend? 如何从快速后端服务器获取图像并将其显示到 React js 前端? - How to Fetch and Display an Image from an express backend server to a React js frontend? React/Node.js:无法从前端和数据库中删除条目 - React/Node.js: Unable to delete an entry from frontend and database 在反应中显示来自 json 数据库的图像 - Display image from json database in react 在React JS中从Django Rest加载图像 - Load image from django rest in React JS 使用 React JS 从数据库中显示图像和视频 - dispay image and video from database with react js 如何在React.js前端应用程序中显示来自Rails 5 API的数据? - How to display data from Rails 5 API in React.js frontend app? 如何显示 object 数组中的一个或多个图像以反应前端购物车部分? - How to display an image or multiple images from an array of object to react frontend cart section? 如何从快速后端服务器获取图像并将其显示到 React Native 前端? - How to Fetch and Display an Image from an express backend server to a React Native frontend? 从数据库 React JS 和 Node JS 显示图像文件 - Displaying Image file from Database React JS and Node JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM