0

All,

I've started this question (http://stackoverflow.com/questions/5016864/include-image-files-in-django-templates-shows-broken-link) with several helpful anserws but somehow this still doesn't resolve my trouble. Therefore i try posting my code and hopefully someone is able to tell me whats going wrong.. I seem to miss something but don't see what:

in settings.py :

MEDIA_ROOT = 'C:/Users/Tijl/Documents/Programming/Dashboard/src/DashboardDesign/FigureOnWebSite/templates/images/static'

MEDIA_URL = 'http://localhost:8000/static/'

In urls.py:

(r'^Point3D/graphics/$', 'FigureOnWebSite.views.graphics'),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                     {'document_root': settings.MEDIA_ROOT}),

in views.py:

> def graphics(request):
>     laptop = "laptop.jpg"
>     t = loader.get_template('FigureOnWebSite/templates/Figure.html')
>     c = Context({'picture': laptop})
>     return HttpResponse(t.render(c))

in my Figure.htm;

<img src"{{picture}}" alt = "picture"/>

it shows a broken link image and if i right click i can see the right source but still isn't displaying it the link is:

http://127.0.0.1:8000/Point3D/graphics/

the location of the file is in: C:\Users\Tijl\Documents\Programming\Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images

If I change Figure.html to;

{{picture}}

it shows laptop.txt on http://127.0.0.1:8000/Point3D/graphics/

Hopefully the above is enough for someone to help me out.

Thanks alot! Tijl

Tijl
  • 13
  • 1
  • 6

1 Answers1

0

This can be answered more easily than your previous question. But you could have just edited your old question.

Solution should be: change your MEDIA_ROOT & MEDIA_URL to

MEDIA_ROOT='C:/Users/Tijl/Documents/Programming/Dashboard/src/DashboardDesign/FigureOnWebSite/templates/images/'
MEDIA_URL='static/'

change your views.py to:

from django.http import RequestContext
from django.shortcuts import render_to_response
def graphics(request):
  laptop = "laptop.jpg"
  c = {'picture': laptop}
  return render_to_response('FigureOnWebSite/templates/Figure.html',c,RequestContext(request))

and your figure.html to:

<img src="{{MEDIA_URL}}{{picture}}" />  

notice: if you would use an ImageFileField in your model, this would all be a lot easier for you. You could just write:

<img src="{{picture.url}}" /> 

and the url you are getting returned is not the "right" url, it is missing the static/ after http://127.0.0.1:8000/. Your correct url must be:

http://127.0.0.1:8000/static/laptop.jpg
marue
  • 5,588
  • 7
  • 37
  • 65
  • Didn't know if changing the question would be noticed, but everyone is very devoted!!I've tried your solution. Think i'm in the next level of errors: it still shows the broken link picture but now there is a warning: Resource interpreted as image but transferred with MIME type text/html. Still no picture but it starts to make sense. – Tijl Feb 16 '11 at 21:14
  • I updated the solution to implement some of the strange aspects of your code. ie. if you use MEDIA_ROOT to point to .../templates/images/static it is looking for files there, so when you tell us that your file is in .../templates/images this is obviously a fault. Hope that clears what MEDIA_ROOT and MEDIA_URL are about MEDIA_ROOT is specifying where to find the static files on your harddisk (the absolute path). MEDIA_URL just tells Django to go to the path specified by MEDIA_ROOT. – marue Feb 16 '11 at 21:25
  • If i run your new code i recieve an other error: unbound method update() must be called with RequestContext instance as first argument (got dict instance instead) any idea why this pops up? I'll see if django explains me something about the ImageFileField Thanks again! – Tijl Feb 16 '11 at 21:34
  • to make that a bit clearer: if MEDIA_ROOT='c:/files/' and your MEDIA_URL='static/' and you enter 'https://127.0.0.1:8000/static/images/picture.jpg' into your browser, django is going to return the file 'c:/files/images/picture.jpg' if it exists. – marue Feb 16 '11 at 21:36
  • Update your code to the updated code in my answer. This is what this site is about: working towards a solution. It's no forum, questions and answers don't have to be static. – marue Feb 16 '11 at 21:37
  • I think the RequestContext should be in: from django.template import RequestContext. Still i recieve a broken link. It redirects to http://127.0.0.1:8000/Point3D/graphics/laptop.jpg instead of http://127.0.0.1:8000/static/Point3D/graphics/laptop.jpg I did change the MEDIA_URL. Manually entering the above link creates an error as well? – Tijl Feb 16 '11 at 21:55
  • The file in Figure.html should be No it works!!!! Thank you so much!!! – Tijl Feb 16 '11 at 22:11
  • Glad you made it. Sorry for the media_url ;), edited my answer to correct that. Now that it works, you can accept my answer with the green checker to the left of the post so everyone can see this question has been answered. – marue Feb 16 '11 at 22:23