【问题标题】:Images from the database not displaying in react when using DRF使用 DRF 时数据库中的图像未在反应中显示
【发布时间】:2023-03-15 05:35:01
【问题描述】:

当数据库中的数据被序列化时,图像在 react 中停止显示,但 {product.image} 显示图像的路径。请我想知道为什么图像没有在反应中显示,但在 django 中运行良好。每当我单击管理页面中的图像链接时,都会显示产品。当从 django 将产品作为 python 数组获取时,显示的图像会做出反应,但是当我转向 drf 时,它会停止显示。我正在使用 Django 4.0,我不知道这是否对这个问题有任何影响。使用 DRF 时,除图像外,产品的所有其他属性都会以反应方式显示

Here's the part of the React component where the image is referenced

 <Link to={`/product/${product._id}`}. 
    <Card.Img src={product.image} />
</Link>
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('base.urls'))
]
 
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATIC_URL = 'static/'
MEDIA_URL = '/images/'  STATICFILES_DIR = [     BASE_DIR / 'static' ] 
MEDIA_ROOT = 'static/images

【问题讨论】:

  • 你能告诉我们产品序列化器吗?一般来说,检查图像字段是否包含在其中或错误地将其放入排除列表中

标签: reactjs django django-rest-framework


【解决方案1】:

我认为你应该给出来源:

<Card.Img src={product.image.url} />

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 2021-03-25
    • 2021-04-04
    • 2021-01-15
    • 2019-07-27
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    相关资源
    最近更新 更多