MEDIA_ROOT MEDIA_URL:通过Django渲染静态服务器页面

时间:2012-04-12 15:37:13

标签: python django

我在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">

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

PRJ / site_media /图像/ 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

1 个答案:

答案 0 :(得分:0)

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

仔细阅读this page

在你的情况下,这将是:


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

要使管理页面正常工作,请在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))