The model is defined as follow:
class Product(models.Model):
title = models.CharField(max_length=50)
summary = models.CharField(max_length=200)
def upload_path(self, filename):
return "product_images/" + self.title.lower().replace(" ", "_") + path.splitext(filename)[1]
img_sum = models.ImageField(upload_to=upload_path)
The view is simply:
class HomeView(generic.ListView):
context_object_name = 'products_list'
template_name = 'core/home.html'
model = Product
And the template tries to display an image :
{% for p in products_list %}
<a href="#">
<article class="product_frame">
<img src="{{ p.img_sum.url }}" />
<h3>{{ p.title }}</h3>
<p>{{ p.summary }}</p>
</article>
</a>
{% endfor %}
The MEDIA_ROOT is explicitly set to
MEDIA_ROOT = '/home/me/workspace/website/www/core/media/'
However, it looks like MEDIA_ROOT
is never appended to the path defined in the model. As a sanity check, here is what I get from the shell for a test Product
instance provided through the django admin interface:
In [20]: p = Product.objects.all()[3]
In [21]: p
Out[21]: <Product: Title : Test -- This is a test>
In [22]: p.img_sum
Out[22]: <ImageFieldFile: product_images/test.jpg>
In [23]: p.img_sum.url
Out[23]: '/media/product_images/test.jpg'
So when I call {{ p.img_sum.url }}
in the template which is located under www/core/templates
, it won't find the image file because the relative url should be ../media/product_images/test.jpg
instead of /media/product_images/test.jpg
.
How can I resolve the good url for my image files ?