我的模板无法显示来自模型的右图 图像未加载到模板上 src属性带有适当的字段但是 没有显示正确的图像
模板
<div class="grid-product">
{% for p in prod %}
<div class=" product-grid">
<div class="content_box"><a href="single.html">
<div class="left-grid-view grid-view-left">
<img class="img-responsive watch-right" alt="not found" src="{{p.photo}}"/>
<div class="mask">
<div class="info">Quick View</div>
</div>
</a>
</div>
<h4><a href="#"></a></h4>
<p>It is a long established fact that a reader{{ p.p_name }}</p>
Rs. 499
</div>
</div>
{% endfor %}
<div id="show"></div>
<div class="clearfix"> </div>
</div>
model
class product(models.Model):
p_id=models.AutoField(primary_key=True)
ps_id=models.ForeignKey(alphasubcat,on_delete=models.CASCADE)
p_name=models.CharField(max_length=100,null=True,blank=True)
photo = models.ImageField(upload_to='productimage',blank=True)
price=models.IntegerField()
def __str__(self):
return self.p_name
view
def produc(request):
param=dict()
cat=categories.objects.all()
sub=subcategories.objects.all()
temp=request.GET['p']
prod=product.objects.filter(ps_id=alphasubcat.objects.get(as_name=temp))
param['prod']=prod
param['cat']=cat
param['sub']=sub
return render(request,"product.html",param)
我也尝试了很多解决方案 可得到 线上 但那不起作用 我的问题
答案 0 :(得分:3)
您可以{{p.photo.url}}
访问图片网址。但就像你的模型一样:
photo = models.ImageField(upload_to='productimage, blank=True)
(空白=真)你想要使用类似的东西:
<img class="img-responsive watch-right" alt="not found" src={% if p.photo %}"{{p.photo.url}}"{% else %}"/something/else.png"{% endif %}/>
答案 1 :(得分:1)
尝试将代码更改为
<img class="img-responsive watch-right" alt="not found" src="{{p.photo.url}}"/>
答案 2 :(得分:0)
在本地服务器上
<img class="img-responsive watch-right" alt="not found" src="127.0.0.1:8000/{{p.photo.url}}"/>
这是settings.py
中的精细设置STATIC_URL = '/static/'
STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static"), ]
STATIC_ROOT=os.path.join(os.path.dirname(BASE_DIR),"static_cdn")
MEDIA_ROOT=os.path.join(os.path.dirname(BASE_DIR),"src/media")
MEDIA_URL="/img/" –
但也调试STATIC_ROOT是返回域/链接到图像
答案 3 :(得分:0)
这对我的问题有用 这里static是我的媒体文件的根文件夹
img class =“img-responsive watch-right”alt =“not found”src =“static / {{p.photo}}”