如何在Reactjs中控制img属性src从数据库中获取数据

如何在Reactjs中控制img属性src从数据库中获取数据,reactjs,django,django-models,axios,Reactjs,Django,Django Models,Axios,我正在创建一个与文章相关的项目。一篇文章可以由多个作者撰写。在这篇文章中,我想在网页上显示图像,如果它有src,如果它没有src,则显示为空。我使用axios计算数据 <img className="userpic" src={item.author[0].picture} alt="" /> <img className="userpic" src={item.author[1].picture} alt="

我正在创建一个与文章相关的项目。一篇文章可以由多个作者撰写。在这篇文章中,我想在网页上显示图像,如果它有src,如果它没有src,则显示为空。我使用axios计算数据

<img className="userpic" src={item.author[0].picture} alt="" />
<img className="userpic" src={item.author[1].picture} alt="" />
<img className="userpic" src={item.author[2].picture} alt="" />

我们如何解决它?

尝试使用默认图像

const image = item.author[0].picture 
<img src={image === 'undifiend'
 ?  // put the path of default image
: 
image} class="img-fluid" />
const image=item.author[0]。图片


基于该错误,您需要执行
{%if item.author[0]}item.author[0]}图片{%endif%}
@markwalker\uu我们如何使用axios在reactjs中实现这一点。这是确保某些内容已定义的常用方法
if(item.author[0]!==未定义){
获取错误
未捕获类型错误:无法读取未定义的属性“picture”
const image = item.author[0].picture 
<img src={image === 'undifiend'
 ?  // put the path of default image
: 
image} class="img-fluid" />
<img className="userpic" src={item.author[0] ? item.author[0].picture : 'default_image_url'} alt="" />