SilentDev
SilentDev

Reputation: 22747

Django - possible to display ImageField objects in static folder?

This is my settings.py:

STATIC_URL = '/static/'

# Additional locations of static files
 STATICFILES_DIRS = (
    '/home/userName/project/app/static',
)

Suppose my view is passing an ImageField object to the template. So as described here in the docs:

https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.fields.files.FieldFile.url

I can access the URL of the image by doing

 {{ FieldFile.url }}

in my template. It works if I try to access the URL of the image, however, I need to be able to load static as well. I want to do something like this:

{% load staticfiles %}

<img src="{% static "{{ FieldFile.url }}" %}" alt="" />

however, that does not work. Suppose the FieldFile.url is

images/imageName.jpg

and when I do

<img src="{% static "images/imageName.jpg" %}" alt="" />

it works, but it doesn't work when I use

{{ FieldFile.url }}

for some reason, how come?

Note: when I try to do

{% load staticfiles %}

<img src="{% static "{{ FieldFile.url }}" %}" alt="" />

and 'inspect element' using google chrome, it shows this

<img src="/static/%7B%7B%20FieldFile.url%20%7D%7D" alt="" />

Upvotes: 0

Views: 990

Answers (1)

Scott Woodall
Scott Woodall

Reputation: 10676

The {{ }} are not necessary since you are already inside a template tag:

<img src="{% static FieldFile.url %}" alt="" />

Upvotes: 4

Related Questions