1

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 ?

Buddyshot
  • 1,614
  • 1
  • 17
  • 44

2 Answers2

0

Try to define the MEDIA_URL in settings.py to '../media/'.

McAbra
  • 2,382
  • 2
  • 21
  • 29
  • What should be its value in this specific case ? I tried '/media/' but it did not work out. – Buddyshot Jul 09 '14 at 22:46
  • Thanks. Now it does not seem to be a problem of relative path. In my template I manually added this : ` ` The first image won't show but the second one will, though both files are definitely present. I really don't get it. – Buddyshot Jul 09 '14 at 23:00
  • So to be clear : the path should be relative to the location of the `views.py`, and in that case the '/media/' is correct for MEDIA_URL (actually I don't even need it). The problem is just that images won't get displayed if they are stored under /media, and they will if they are under /static. – Buddyshot Jul 09 '14 at 23:04
  • When debug = True the behavior of static (media) serving is slighty different. [Try out those docs.](https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development) – McAbra Jul 10 '14 at 13:33
0

So the problem was not related to file location but rather to the fact that Django does not serve media files by default in the dev environment. This question helped to figure it out.

Community
  • 1
  • 1
Buddyshot
  • 1,614
  • 1
  • 17
  • 44