如何在Django 1.0中使用静态文件地址
2008-09-04 14:48 Jaypei 阅读(1534) 评论(0) 编辑 收藏 举报
Django本身是不支持静态文件的,网上找了些资料,用Django自带的static.py解决了问题。
How to do it
在URLconf中如下代码:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
site_media是URL根地址,/path/to/media是网站文件目录根地址。
比如:
- 文件 /path/to/media/foo.jpg 在URL /site_media/foo.jpg 可用.
- 文件 /path/to/media/css/mystyles.css 在URL /site_media/css/mystyles.css 可用.
- 文件 /path/bar.jpg 访问不到, 因为它不在规则范围内.
目录列表
设置serve的show_indexes参数可以在访问指定URL根地址的时候显示文件列表。
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media', 'show_indexes': True}),
可以定义static/directory_index自定义显示文件列表的样式,参数如下:
directory-目录名称, file_list-文件列表
也可以复制static.py文件到项目文件中做显示功能上的定制(如文件大小等)。
例子1:添加 static/directory_index文件
# ......
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<meta name="robots" content="NONE,NOARCHIVE" />
<title>Index of {{ directory|escape }}</title>
</head>
<body>
<h1>Index of {{ directory|escape }}</h1>
<ul>
{% for f,f_size in file_list.items %}
<li><a href="{{ f|urlencode }}">{{ f|escape }}</a> <span style="color:green">{{ f_size }} bytes</span></li>
{% endfor %}
</ul>
</body>
</html>
"""
def directory_index(path, fullpath):
try:
t = loader.get_template('static/directory_index')
except TemplateDoesNotExist:
t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default directory index template')
files = {}
for f in os.listdir(fullpath):
if not f.startswith('.'):
f_size = 0
full_name = os.path.join(fullpath, f)
if os.path.isdir(full_name):
f += '/'
else:
f_size = os.stat(full_name)[6]
files[f] = f_size
c = Context({
'directory' : path + '/',
'file_list' : files,
})
return HttpResponse(t.render(c))
# ......
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<meta name="robots" content="NONE,NOARCHIVE" />
<title>Index of {{ directory|escape }}</title>
</head>
<body>
<h1>Index of {{ directory|escape }}</h1>
<ul>
{% for f,f_size in file_list.items %}
<li><a href="{{ f|urlencode }}">{{ f|escape }}</a> <span style="color:green">{{ f_size }} bytes</span></li>
{% endfor %}
</ul>
</body>
</html>
"""
def directory_index(path, fullpath):
try:
t = loader.get_template('static/directory_index')
except TemplateDoesNotExist:
t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default directory index template')
files = {}
for f in os.listdir(fullpath):
if not f.startswith('.'):
f_size = 0
full_name = os.path.join(fullpath, f)
if os.path.isdir(full_name):
f += '/'
else:
f_size = os.stat(full_name)[6]
files[f] = f_size
c = Context({
'directory' : path + '/',
'file_list' : files,
})
return HttpResponse(t.render(c))
# ......
使用例二的时候不要直接修改django/view/static.py,最好复制到项目中做修改然后在urls.py中使用项目中的static.py
仅当DEBUG=True的时候使用
可是仅在DEBUG=True的时候开启目录浏览功能:
# import 项目setting
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
)
参考资料: