我已经浏览了Django文档和SO问题。 我试图在本地开发服务器上提供css和图像。我相信我有settings.py和urls.py更正:
settings.py:
MEDIA_ROOT = os.path.join(PROJECT_PATH,'media/')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
#MEDIA_URL = ''
MEDIA_URL = 'http://localhost:8000/media/'
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)', 'django.views.static.serve', {
'document_root': '/media/'
}),
)
检查标题我得到了这个: http://localhost:8000/media/style.css
GET /media/style.css HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0
Accept: text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Referer: http://localhost:8000/hotels/select_hotel
Cookie: SESS382051e6e1ed8806a7f7ff8f96fa26b4=imttngg6l6ks88q89fgabsn0d3; has_js=1
Cache-Control: max-age=0
HTTP/1.0 404 NOT FOUND
Date: Tue, 13 Dec 2011 15:41:38 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: text/plain
Content-Length: 32
- 那么我哪里出错?
这是我的目录:
drwxr-xr-x 11 jgoldstick jgoldstick 4096 2011-12-13 11:06 ..
-rw-r--r-- 1 jgoldstick jgoldstick 38925 2011-12-13 10:50 headers.txt
drwxr-xr-x 2 jgoldstick jgoldstick 4096 2011-12-13 10:50 .
-rw-r--r-- 1 jgoldstick jgoldstick 6948 2011-12-13 07:56 edynamic_logo.gif
-rw-r--r-- 1 jgoldstick jgoldstick 24 2011-12-12 13:18 style.css
jgoldstick@jgoldstick-desktop:~/code/python/django/ETG_Offer_entry/media$ pwd
/home/jgoldstick/code/python/django/ETG_Offer_entry/media
答案 0 :(得分:1)
'document_root'
键的值必须是完整的文件系统路径(即MEDIA_ROOT
),而不仅仅是'/media/'
。
答案 1 :(得分:0)
在urls.py中你应该使用MEDIA_ROOT而不是'/ media /'作为document_root参数,如果没有检查MEDIA_ROOT是否存在(并且你有权访问它)
注意:MEDIA_ROOT应该是绝对路径
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
'show_indexes': True
}),
)
在settings.py 中
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
答案 2 :(得分:0)
其他人提供了答案,但你应该真的:
当你转向生产时,两者都会有很长的路要走。