Take the 2-minute tour ×

I'm having trouble getting my image files to appear from the database info. If I type the filename directly, it works fine, but the code as it is shows a broken image. The non-image variables work fine, and the image is stored in the model as a CharField of the filename (which I realize now may not have been best, but I think it may be too late to change?) What am I doing wrong?

<div class="product_image" >
        {% load static %} <img src="{% static "images/{{p.image.url}}" %}" alt={{p.name}}/>

(I've also tried {{p.image}} with no luck.)

Here are the relevant settings--still confused about media vs static.

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')

# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'staticcoll')

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__), 'static'),

Here is the Product model (p):

class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    price = models.DecimalField(max_digits=9,decimal_places=2)
    old_price = models.DecimalField(max_digits=9,decimal_places=2,
    image = models.CharField(max_length=50, default="imagenotfound.jpeg")
    is_active = models.BooleanField(default=True)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category)
    store_name = models.ForeignKey(Store, blank = True, null = True)
    class Meta:
        db_table = 'products'
        ordering = ['-created_at']

    def __unicode__(self):
        return self.name
share|improve this question
I have definitely been here before when starting out with Django. Why are you using a CharField to store the image? Is it a full or relative path or URL? –  willOEM Jan 24 '13 at 18:01
I'm not sure why I did it that way, other than I'm still learning. When I view the page source after loading, I get: <img src="/static/images/{{p.image}}" alt=X-Men Action Figures/> Which surprised me; I had thought I would get a full path. Nonetheless, a similar static image elsewhere--where I typed the name of the file rather than using a variable--worked fine. Does it have something to do with the variable being inside the string, so it doesn't get rendered? I tried putting it outside the quotes but it raised a SyntaxError. –  thumbtackthief Jan 24 '13 at 18:05
If you're using south you could write a custom migration to convert the char field to an ImageField –  Nathaniel Jan 24 '13 at 18:15
I'll second @Nathaniel, if you are not using South you should start. It will provide a pretty painless way for you to convert your image CharField to an ImageField. –  willOEM Jan 24 '13 at 18:38
I will check it out, but I have to submit this code for an interview tomorrow, and it's far from done. Considering I've only been doing Django for four days, the list of things to learn before tomorrow is already quite long... –  thumbtackthief Jan 24 '13 at 18:43
add comment

2 Answers

up vote 1 down vote accepted

Since it's just the file name stored in p.image.url, this will work:

<img src="{{ STATIC_URL }}images/{{p.image}}" alt={{p.name}}/>

You must have context = RequestContext(request) in your view for {{ STATIC_URL }} to work.

You can read about RequestContext here.

share|improve this answer
Still doesn't work... –  thumbtackthief Jan 24 '13 at 18:16
I'll check--should I include the {% load static %} or no? (Tried both and neither worked) –  thumbtackthief Jan 24 '13 at 18:18
In your settings.py, do you have 'django.core.context_processors.static' in your TEMPLATE_CONTEXT_PROCESSORS? If not, that would cause the {{ STATIC_URL }} not to load. –  Dan Hoerst Jan 24 '13 at 18:33
Also, see EDIT2 of my original post. If you are not using RequestContext when rendering your template, then STATIC_URL will not work. –  Dan Hoerst Jan 24 '13 at 18:36
let us continue this discussion in chat –  Dan Hoerst Jan 24 '13 at 18:47
show 8 more comments

You can't nest the Django tags like that.

If p.image is a CharField, use

<img src="{% static p.image %}" alt="{{p.name}}"/>

You'll need to make sure the correct path is stored in the field.

share|improve this answer
The file is stored in static/images. Does that matter? It's not working as is. –  thumbtackthief Jan 24 '13 at 18:11
add comment

Your Answer


