我試圖從媒體目錄將JPG圖像加載到我的Django服務器。TypeError嘗試訪問靜態媒體目錄上的任何文件
我沒有問題從STATIC_ROOT加載CSS,但我無法從MEDIA_ROOT訪問圖像既不css。無論是從應用程序或從瀏覽器
我使用PILL庫進行縮略圖和抗鋸齒img保存之前,上傳運行良好,圖像複製到我的MEDIA_ROOT。但該應用程序仍然無法訪問它。瀏覽器中的調試工具(Firebug)聲明'圖像加載失敗',但鏈接內的網址完全正確。
這是我在嘗試打開該文件到瀏覽器的錯誤:
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
URL被存儲到img
變量一個Unicode字符串。
應用程序可能會如何正確寫入模板,圖像是否存在,MEDIA_URL是否設置並正常工作,但圖像是否仍未加載到瀏覽器中?
這裏是我的「urls.py」作爲懇請:
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")}),
)
當我瀏覽這個觀點應用程序運作良好,並上傳PIC,但圖像無法加載到視圖和其他視圖也:
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))
感謝您的時間
異常請更新您的觀點,即觸發異常,並充分例外回溯問題。 – 2012-03-24 17:35:05
如果您使用Django提供靜態文件,請提供'settings.py'和'urls.py'。 – 2012-03-24 20:19:46
我會發布我的'urls.py'。 'setting.py'是'django-admin'給出的標準。並且靜態文件的變量設置正確,因爲文件被上傳。 – LorenzoDaPonte 2012-03-25 00:29:10