TypeError while trying to access any file on static media directory
Question
I'm trying to load a JPG image from the media directory into my Django server.
I have no problem loading css from the STATIC_ROOT but i cannot access images neither css from the MEDIA_ROOT. Neither from the app nor from the browser
I'm using the PILL library to make a thumbnail and antialiasing the img before saving it, the upload runs good, and the image is copied into my MEDIA_ROOT. But the app still cannot access it. The debug utility into the browser (Firebug) states that 'the image failed to load', but the url inside the link is totally correct.
This is the error i get trying to open the file into the browser:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/media/images/django_img.jpg
Django Version: 1.4c1
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'django.contrib.admin',
'todo',
'blog',
'forum')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\static.py" in serve
48. fullpath = os.path.join(document_root, newpath)
File "C:\Python27\lib\ntpath.py" in join
96. assert len(path) > 0
Exception Type: TypeError at /media/images/django_img.jpg
Exception Value: object of type 'NoneType' has no len()
The server tells the problem is in C:\Python27\lib\ntpath.py in join.
At line 96 <code>
# Join, and ensure there's a separator.
assert len(path) > 0
The url is a unicode string stored into img
variable.
How is possible that the app write the template right, the image is there, the MEDIA_URL is set and working correctly, but still the image is not loaded into the browser?
Here's my 'urls.py' as kindly asked:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.defaults import *
from django.utils.translation import ugettext as _
import os
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r"^forum/(\d+)/$", "forum.views.forum"),
(r"^thread/(\d+)/$", "forum.views.thread"),
(r"^post/(new_thread|reply)/(\d+)/$", "forum.views.post"),
(r"^reply/(\d+)/$", "forum.views.reply"),
(r"^new_thread/(\d+)/$", "forum.views.new_thread"),
(r"^profile/(\d+)/$", "forum.views.profile"),
(r'^media/(?P<path>.*)$', 'django.views.static.serve'),
(r"", "forum.views.main"),
)
if settings.DEBUG:
# serving the media files for dojango / dojo (js/css/...)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(os.path.dirname(__file__), "media")}),
)
When I browse this view the app works well and upload the pic, but the image fail to load into the view and in the other views also:
def profile(request, pk):
profile = UserProfile.objects.get(user=pk)
img = None # I tried to insert here an empty string but no result
if request.method == "POST":
pf = ProfileForm(request.POST, request.FILES, instance=profile)
if pf.is_valid():
pf.save()
# resize and save image under same filename
imfn = pjoin(MEDIA_ROOT, profile.avatar.name)
im = PImage.open(imfn)
im.thumbnail((160,160), PImage.ANTIALIAS)
im.save(imfn, "JPEG")
else:
pf = ProfileForm(instance=profile)
if profile.avatar:
img = "/media/" + profile.avatar.name
return render_to_response("forum/profile.html", add_csrf(request, pf=pf, img=img))
Thanks for your time
Solution
You have 2 calls to django.views.static.serve
in your urls, first one is not provided with document_root
so it's assumed None
and that raises an exception on os.path.join