我正在尝试将媒体目录中的JPG图像加载到我的Django服务器中。
我从STATIC_ROOT加载css没有问题,但是我无法从MEDIA_ROOT访问图像。既不是来自应用程序也不是来自浏览器
我正在使用PILL库在保存之前制作缩略图并对img进行抗锯齿处理,上传运行良好,并将图像复制到我的MEDIA_ROOT中。但该应用程序仍然无法访问它。浏览器中的调试实用程序(Firebug)声明“图像无法加载”,但链接中的URL完全正确。
这是我试图将文件打开到浏览器中的错误:
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')
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()
url是存储在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
变量中的unicode字符串。
应用程序如何正确编写模板,图像在那里,MEDIA_URL已设置且工作正常,但图像是否仍未加载到浏览器中?
这是我的'urls.py',请问:
img
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")}),
)
当我浏览此视图时,该应用程序运行良好并上传图片,但图片无法加载到视图中,而在其他视图中也是:
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")}),
)
def profile(request, pk):
感谢您的时间
答案 0 :(得分:0)
您在网址中有django.views.static.serve
次来电,第一次未提供document_root
,因此假设为None
,并在os.path.join
上引发了异常