4

My template cannot display the image coming from my model.

src attribute comes with proper field but it does not display the proper image

template:

       <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)
Tms91
  • 3,456
  • 6
  • 40
  • 74
Jigar Tarpara
  • 59
  • 1
  • 10

4 Answers4

6

you can access image url by {{p.photo.url}}. but as here in your model:

photo = models.ImageField(upload_to='productimage, blank=True)

(blank=True) you would want to use something like:

<img  class="img-responsive watch-right" alt="not found" src={% if p.photo %}"{{p.photo.url}}"{% else %}"/something/else.png"{% endif %}/>
varnothing
  • 1,269
  • 1
  • 17
  • 30
  • nice shot. One question: I get `tag start is not closed` as syntax error after closing quotes of `"{{p.photo.url}}"`. Any ideas? – stelios Jul 31 '17 at 19:56
  • 1
    @chefarov it might be a simple mismatch of opening and closing quotes as you know quotes also behave as brackets. If you can share your code, I can look into it. – varnothing Aug 07 '17 at 09:20
1

Try changing your code to

 <img  class="img-responsive watch-right" alt="not found" src="{{p.photo.url}}"/>
ilse2005
  • 11,189
  • 5
  • 51
  • 75
0

On your local server

<img  class="img-responsive watch-right" alt="not found" src="127.0.0.1:8000/{{p.photo.url}}"/>

thats fine settings in 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/" –

but also debug that STATIC_ROOT is return domain/link-to-image

SuReSh
  • 1,503
  • 1
  • 22
  • 47
0

this is work for my problem here static is my root folder for media file

img  class="img-responsive watch-right" alt="not found" src="static/{{p.photo}}"
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Jigar Tarpara
  • 59
  • 1
  • 10