I am having trouble in django generating a page that displays both text and a .png image.
Within views.py a function as per below works fine
def image_test(request):
image_file = open("C:\\temp\\test.png",'rb').read()
return HttpResponse(image_file,mimetype='image/png')
now if i try to create html that will display both text and the image (as below)
def test(request):
html = "<html><body> Hi there.\n\n"
html += "<img src = \"C:\\temp\\test.png\" type =image/png /> \n"
html += "</body></html> \n"
return HttpResponse(html)
then only the text displays. However if i save the generated html to a file as per below
<html><body> Hi there.
<img src = "C:\temp\test.png" type =image/png />
</body></html>
and open that in the browser then it displays both the text and the png file.
It’s not really a Django issue. When you open a webpage from an external server (ie. not directly from your hard drive, even if the server runs on your local machine) your browser doesn’t allow it to reference files on your hard drive.
You need to change the images URL so it references URL on the server, not on your hard disk.
Read about serving static files in Django and about using templates.