I use Django 1.9.1, Python 3.5. Code:
class Image(models.Model):
image = models.ImageField(upload_to = "shop/static/shop")
description = models.CharField(max_length = 200, blank = True)
def __str__(self):
return self.image.url
def image_tag(self):
return u'<img src="/%s" width="300"/>' % self.image.url[5:] #Bad code
image_tag.allow_tags = True
When object Image is created then Image.image.url=shop/static/shop/filename. I can get the image on following address: /static/shop/filename. This example is working but
return u'<img src="/%s" width="300"/>' % self.image.url[5:] #Bad code
self.image.url[5:] is bad code I think. If STATIC_URL will be changed then the code won't work. How is it possible to change the code for working with other STATIC_URL. Does exist a library handling URL?
STATIC_URLbaked in the database records?<img>tag for example. I don't know if mySTATIC_URLbaked in the database records. TheSTATIC_URLis insettings.py.instance.image.urlis completely valid. More than that, it automatically changes if you change thesettings.STATIC_URL. Maybe you need something likemanage.py collectstaticto move your static files to the proper location?shop/static/shop(upload_toargument). Andinstance.image.urlshows served path + filename:shop/static/shop/filename. This url isn't valid. This is path. Expected valid url isstatic/shop/filename.settings.STATIC*variables.