Reputation: 20838
I'm trying to get an image to come through in my Django Template. Can anyone help?
Here is my Template code:
<img src={{myImage}}/>
Here is my view code:
def base_book(request):
theBook = Book.objects.get(title__contains="django")
theAuthors = Book.objects.get(id=2)
myAuthors = theAuthors.authors.all()
myImage = "{{STATIC_URL}}two_scoops_django.png"
return render(request, 'base_book.html', {'book': theBook, 'author': myAuthors, 'myImage': myImage})
I have the image file "two_scoops_django.png" saved in an image directory on my file path in my settings like so:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templatesDir'),
os.path.join(BASE_DIR, 'imageDir'),
)
I have tried several different combinations trying to get it to come through, but it hasn't worked yet.
Here is my URL pattern as well if it helps:
# DB - generated URL
url(r'^base_book/$', base_book),
Thank you in advance!
Upvotes: 0
Views: 51
Reputation: 77942
The {{ STATIC_URL }}
stuff will only work in the template. You have to change your template to
<img src="{{ STATIC_URL }}{{myImage}}"/>
and you view code to
def base_book(request):
(...)
myImage = "two_scoops_django.png"
return render(request, 'base_book.html',
{'book': theBook, 'author': myAuthors, 'myImage': myImage}
)
Upvotes: 3