I am very new to Django and trying display an image on an html page by passing the path to the src from the view.
I have this in the 'task1.html' file
<figure><img src={% static '{{ main_figure }}' %}></figure>
<p>{{ description }}</p>
and this in the 'views.py' file
def task1(request):
return render(
request,
"App/Tasks/task1.html",
{
'main_figure' : "assets/placeholder_img.png",
'description' : "Placeholder image and text"
}
)
But the image does not show. If I remove the 'main_figure' : "assets/placeholder_img.png",
line and change
<figure><img src={% static '{{ main_figure }}' %}></figure>
to <figure><img src={% static 'assets/placeholder_img.png' %}></figure>
the image displays, so the issue seems to be with the views function.
I have tried passing different parts of the src from the view, e.g.
<img src={{ main_figure }}>
with 'main_figure' : "{% static 'assets/placeholder_img.png' %}",
and
<img src={% static 'assets/{{ main_figure }}.png' %}>
with 'main_figure' : "placeholder_img",
but it always shows the little image not found icon.
Any help would be greatly appreciated, thanks very much.
{{xxx}} is just printing a variable xxx (so what is inside {{ }}) therefore no spaces are needed.
{% xxx %} is executing a tag and spaces before/after % are necessary
if you need to use a variable inside tag no need to use {{ }}: {% static main_figure %}