Django:如何添加静态文件 [英] Django: How Add Static Files

查看:127
本文介绍了Django:如何添加静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

把所有这些主题都改写成 https://stackoverflow.com/search?q=django.views.static .serve
并没有帮助:($ / b
$ b操作系统:Windows XP
Python:2.7
Django:1.2。 3

媒体

D:\ bugtracker \static

带有档案:
docicons-note.gif

style.css


settings.py 中设置:
MEDIA_ROOT ='D:/ bugtracker / static /'

MEDIA_URL ='/


urls.py 中设置:

  urlpatterns = patterns('',
(r'^ media /(?P。*)$','django.views.static.serve', {'document_root':'settings.MEDIA_ROOT'}),

(在< tag>中没有空格)
<!DOCTYPE html>
< html lang ='ru'>

< head>

< link rel =stylesheettype = text / csshref =media / style.css/>



< body>

< img src ={{MEDIA_URL}} / docicons-note.gif/>

< / body>


解决方案

urls.py:

  urlpatterns = patterns('',
(r'^ test_media /(?P< path>。*) $','django.views.static.serve',
{'document_root':'settings.MEDIA_ROOT'}),


settings.py
MEDIA_ROOT ='路径/到/ media /文件夹/'

所以如果你有/test_media/photo.jpg将被使用MEDIA_ROOT的值:
'path / to / media / folder / photo.jpg'

模板文件

 < !DOCTYPE html> < html lang ='ru'> 
<头>
< link rel =stylesheettype =text / csshref =/ test_media / style.css/>
< /头>
<体>
< img src =/ test_media / docicons-note.gif/>
< /体>
< / HTML>

这只能用于开发目的。对于生产你应该使用一个真正的Web服务器。

Readed all these topic https://stackoverflow.com/search?q=django.views.static.serve And it not helped :(

OS: Windows XP Python: 2.7 Django: 1.2.3

Media in
D:\bugtracker\static

With files:
docicons-note.gif
style.css

In settings.py i set:
MEDIA_ROOT = 'D:/bugtracker/static/'
MEDIA_URL = '/static/'

In urls.py i set:

urlpatterns = patterns('',
    (r'^media/(?P.*)$', 'django.views.static.serve',{'document_root':'settings.MEDIA_ROOT'}),
)

template: (read it without space in < tag>)
< !DOCTYPE html> < html lang='ru'>
< head>
< link rel="stylesheet" type="text/css" href="media/style.css" />
< /head>
< body>
< img src="{{MEDIA_URL}}/docicons-note.gif"/>
< /body>
< /html>

解决方案

urls.py:

urlpatterns = patterns('',
    (r'^test_media/(?P<path>.*)$', 'django.views.static.serve',   
        {'document_root':'settings.MEDIA_ROOT'}),
)

settings.py MEDIA_ROOT = 'path/to/media/folder/'

So if you have /test_media/photo.jpg will be used the value from MEDIA_ROOT: 'path/to/media/folder/photo.jpg'

template file:

< !DOCTYPE html> < html lang='ru'>
< head>
< link rel="stylesheet" type="text/css" href="/test_media/style.css" />
< /head>
< body>
< img src="/test_media/docicons-note.gif"/>
< /body>
< /html> 

This should be used only for development purposes. For production you should use a real web server.

这篇关于Django:如何添加静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆