0

I am trying to load some images into a template but it continues to appear as broken link My files and directories are as follows 1.views.py

def home(request):
    #template = loader.get_template("polls/home.html")
    return render_to_response('polls/home.html',
                              context_instance=RequestContext(request))

2.urls.py

urlpatterns = patterns('',
    #url(r'^applications/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
    (r'^', include('polls.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

3.home.html

{% load staticfiles %}
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>psdtowebSimpleWebsiteTemplate.psd</title>
        <link href="{% static 'C:\Users\omars_000\Desktop\mytask\polls\templates\polls\styles.css' %}" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div id="background">
            <div id="background"><img src="{{MEDIA_URL}}/Capture.png"></div>
 </body>
 </html>

4.settings.py

MEDIA_ROOT = os.path.join( PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, "static"),
    '/var/www/static/',
)

Any idea why it is not working? I have checked many questions but nothing helped so far.

omarsafwany
  • 3,695
  • 8
  • 44
  • 75

2 Answers2

0

Your MEDIA_URL already has a trailing slash. You are adding another in <img src="{{MEDIA_URL}}/Capture.png"> Most browsers don't care, but perhaps yours does.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
0

Got it. In home.html I fixed to be as follows:

<div id="background"><img src="{% static "C:\Users\omars_000\Desktop\mytask\mytask\media\images\background.png" %}""></div>

and in settings.py I added the location of the images i will be using in the template as follows:

STATICFILES_DIRS = (
    'C:\Users\omars_000\Desktop\mytask\mytask\media\images',
    'C:\Users\omars_000\Desktop\mytask\mytask\media',
)
omarsafwany
  • 3,695
  • 8
  • 44
  • 75