amishrv55
amishrv55

Reputation: 37

media Image url not opening in django template

Image from media not opening in template in my django app. below is urls or my project

from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('myroyalkennel', include('myroyalkennel.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT) 

Below is my settings.py

STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/' 
def store (request):
    items = products.objects.all
    return render(request, "myroyalkennel/store.html", {"itms":items}) 

below is my template:

<body>
    <table>
        <tr>
        <th>image</th>  
        <th>SERIAL NUMBER</th>    
        <th>SERIAL NUMBER</th>
        <th>PRODUCT NAME</th>
        <th>VARIENT</th>
        <th>MRP</th>
        <th>DISCOUNTED PRICE</th>
        <th>DISCOUNT PERCENT</th>
        <th>IN STOCK</th>
        </tr>
    {%if itms %}
    {% for item in itms %}
        <tr>
            <td>{{item.image.url}}</td>
            <td>{{item.Serial_number}}</td>
            <td>{{item.product_name}}</td>
            <td>{{item.Varient}}</td>
            <td>{{item.MRP}}</td>
            <td>{{item.Discounted_price}}</td>
            <td>{{item.Discount_percent}}</td>
            <td>{{item.In_Stock}}</td>
        </tr>
        {% endfor %}
        {% endif %}    
    </table>
</body>
class products(models.Model):
    Serial_number = models.CharField(max_length=10)
    product_name = models.TextField()
    Varient = models.CharField(max_length=15)
    MRP = models.IntegerField()
    Discounted_price = models.IntegerField()
    Discount_percent = models.CharField(max_length=6)
    In_Stock = models.CharField(max_length=15)
    image = models.ImageField(upload_to="asset/image",default="")

    def __str__(self):
        return self.product_name

When I am calling the image in template with {{ item.image.url}} it gives path /media/asset/image/rkci_logo.jpg but image does not open.

Upvotes: 0

Views: 364

Answers (1)

Ajay Lingayat
Ajay Lingayat

Reputation: 1673

You are just printing the image url into the template. You should define a img tag and set the src attribute to {{item.image.url}}.

Try this:

store.html

<body>
    <table>
        <tr>
        <th>image</th>  
        <th>SERIAL NUMBER</th>    
        <th>SERIAL NUMBER</th>
        <th>PRODUCT NAME</th>
        <th>VARIENT</th>
        <th>MRP</th>
        <th>DISCOUNTED PRICE</th>
        <th>DISCOUNT PERCENT</th>
        <th>IN STOCK</th>
        </tr>
    {%if itms %}
    {% for item in itms %}
        <tr>
            <!-- see below -->
            <td><img src="{{item.image.url}}" /></td>
            <td>{{item.Serial_number}}</td>
            <td>{{item.product_name}}</td>
            <td>{{item.Varient}}</td>
            <td>{{item.MRP}}</td>
            <td>{{item.Discounted_price}}</td>
            <td>{{item.Discount_percent}}</td>
            <td>{{item.In_Stock}}</td>
        </tr>
        {% endfor %}
        {% endif %}    
    </table>
</body>

Upvotes: 1

Related Questions