1

我的 settings.py 文件中有以下设置:

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}),

在我的网页中:

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

我可以在项目路径中看到该文件:

项目/site_media/images/logo.gif

但是当我在 HTML 页面中开始我的项目时,我仍然看不到图像:

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.

我不确定问题是什么,我该如何解决?

下面是我在 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

服务器日志:

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

请注意,即使 CSS / JS 脚本也没有被加载:

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
4

1 回答 1

0

由于您使用的是 Django 1.3.1,而不是 MEDIA_ROOT 和 MEDIA_URL,我建议使用 django.contrib.staticfiles

仔细阅读本页

在你的情况下,这将是这样的:


设置.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",
)

为了保持管理页面正常工作,在 urls.py

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

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

    # Your other url code
)

然后在您的网页上,您可以输入:

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

请记住,您必须使用正确的上下文处理器来呈现您的模板:

return render_to_response('my_page.html', locals(),
        context_instance=RequestContext(request))
于 2012-04-12T18:57:39.637 回答