1

I have below settings in my settings.py file:

ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(ROOT_PATH,'site_media')
MEDIA_URL = 'http://xx.xxx.xx.xx:yyyy/site_media/'

And in urls.py

urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

And in my web page:

<img border="0" width="120" align="Left" src="/site_media/images/logo.gif">

I can see the file in the project path:

Prj/site_media/images/logo.gif

But still when I start my project in the HTML page I am not able to see the image:

Prj > python manage.py runserver xx.xxx.xx.xx:yyyy
Validating models...

0 errors found
Django version 1.3.1, using settings 'Prj.settings'
Development server is running at http://xx.xxx.xx.xx:yyyy/
Quit the server with CONTROL-C.

I am not sure what the issue is and how can I resolve it?

Below is the Request / Response headers that I see in Firebug:

GET http://xx.xxx.xx.xx:yyyy/site_media/images/logo.gif


Response Headers
Content-Type    text/html; charset=utf-8
Date    Thu, 12 Apr 2012 17:00:01 GMT
Server  WSGIServer/0.1 Python/2.7.2

Request Headersview source
Accept  image/png,image/*;q=0.8,*/*;q=0.5
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Host    xx.xxx.xx.xx:yyyy
Referer http://xx.xxx.xx.xx:yyyy/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0) Gecko/20100101 Firefox/7.0

Server logs:

[12/Apr/2012 09:35:11] "GET /site_media/images/logo.gif HTTP/1.1" 200 122202

Note even the CSS / JS scripts are also not getting loaded:

GET jquery-1.4.2.js

200 OK xx.xxx.xx.xx:yyyy 119.3 KB xx.xxx.xx.xx:yyyy 6.96s

HeadersResponseCache
Response Headersview source
Content-Type    text/html; charset=utf-8
Date    Thu, 12 Apr 2012 17:00:03 GMT
Server  WSGIServer/0.1 Python/2.7.2


Request Headersview source
Accept  */*
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Host    xx.xxx.xx.xx:yyyy 
Referer http://xx.xxx.xx.xx:yyyy/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0) Gecko/20100101 Firefox/7.0
2
  • what is the server log saying when you make a request to your website? What does firebug say when you are loading your webpage?
    – dm03514
    Apr 12, 2012 at 15:50
  • I have added the content as seen via FireBug
    – Programmer
    Apr 12, 2012 at 16:05

1 Answer 1

0

Since you're using Django 1.3.1, instead of MEDIA_ROOT and MEDIA_URL, I'd suggest using django.contrib.staticfiles

Carefully read this page

in your case this would be something like:


settings.py:

STATIC_ROOT = os.path.join(ROOT_PATH,'site_media')
STATIC_URL = '/site_media/'

INSTALLED_APPS = (
    # Your installed apps....
    # ...
    'django.contrib.staticfiles',
)

# Not sure what exact context processors you need, but "static" will be one of them
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",
)

To keep the admin page working, in urls.py

# Admin
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Admin:
    (r'^admin/', include(admin.site.urls)),

    # Your other url code
)

And then on your webpage you can put:

<img border="0" width="120" align="Left" src="{{ STATIC_URL }}images/logo.gif">

Keep in mind you'll have to render your templates using the right context processors:

return render_to_response('my_page.html', locals(),
        context_instance=RequestContext(request))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.