Vaibhav Jain
Vaibhav Jain

Reputation: 5507

image not displaying in django admin site

I have this model:

projectDirPath = path.dirname(path.dirname(__file__)) 
storeImageDir = FileSystemStorage(location=projectDirPath + '/couponRestApiApp/stores')

class stores(models.Model):
    """ This is the store model """
    storeName = models.CharField(max_length=15)                                          # Store Name
    storeDescription = models.TextField()                                                # Store Description
    storeURL = models.URLField()                                                         # Store URL
    storePopularityNumber = models.IntegerField(max_length=1)                            # Store Popularity Number  
    storeImage = models.ImageField(upload_to="images",storage=storeImageDir)            # Store Image 
    storeSlug = models.CharField(max_length=400)                                         # This is the text you see in the URL
    createdAt = models.DateTimeField(auto_now_add=True)                                  # Time at which store is created
    updatedAt = models.DateTimeField(auto_now=True)                                      # Time at which store is updated
    storeTags = models.ManyToManyField(tags)                                             # All the tags associated with the store

    def __unicode__(self):
        return unicode(self.storeName)

    def StoreTags(self):
        return '\n'.join([s.tag for s in self.storeTags.all()])
    def StoreImage(self):    
        return '<img src="%s" height="150"/>' % (self.storeImage)
    StoreImage.allow_tags = True

But image is not loading on the admin page and the image URL is : http://localhost:8000/admin/couponRestApiApp/stores/static/mcDonalds.jpg/

is showing but the correct path should be: /home/vaibhav/TRAC/coupon-rest-api/couponRestApi/couponRestApiApp/stores/static/mcDonalds.jpg/

Where should the images must be stored so that they will be displayed on the Django admin Page

Upvotes: 0

Views: 6651

Answers (3)

Jacob Gokul
Jacob Gokul

Reputation: 172

call global url in your HTML code like {{name.store.url}} it should be done on your HTML code

Upvotes: 0

Devin B.
Devin B.

Reputation: 453

As per Django documention, for Django 1.11 and above you will need to rewrite:

urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 2

arulmr
arulmr

Reputation: 8836

Define MEDIA_ROOT and MEDIA_URL in your settings properly.

settings.py

import os
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8'))

MEDIA_ROOT = os.path.join(CURRENT_PATH, 'media').replace('\\','/')

MEDIA_URL = '/media/'

models.py

storeImage = models.ImageField(upload_to="images")

urls.py

from django.conf import settings
urlpatterns += patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)

Try using the above code.


Answer for question asked in comment:

's' is being added to the model name, since there will be multiple model instances. To get rid of it, define verbose_name for the model.

class stores(models.Model):
    .....
    storeName = models.CharField(max_length=15) 
    .....

    class Meta:
        verbose_name        = 'Store'
        verbose_name_plural = 'Stores'

Upvotes: 5

Related Questions