[转]django设置静态文件路径方法

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://yeelone.blog.51cto.com/1476571/863583

 折腾了一个晚上,查了好多文章,都没有用。最后拼拼凑凑,总算是可以了。以下总结方法:

 

  1. vim mysite/settings.py 
  2.  
  3. 加入: 
  4. HERE = os.path.dirname(os.path.dirname(__file__)) 
  5. 修改: 
  6. MEDIA_ROOT = os.path.join( HERE ,'media').replace('\\','/') 
  7. MEDIA_URL = '/media/' 
  8. STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/') 
  9. STATIC_URL = '/static/' 
  10.  
  11. STATICFILES_DIRS = ( 
  12.    os.path.join(HERE,'app名字/static/').replace('\\','/'), 
  13. 推荐的做法是将静态文件保存在app下的static目录中。 

 

  1. vim mysite/urls.py 
  2. from django.conf.urls.static import static 
  3.  
  4. urlpatterns += static(settings.MEDIA_URL , document_root = settings.MEDIA_ROOT ) 
  5. urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT ) 

 

  1. 然后这样使用: 
  2.  
  3.  
  4.         <link  type="text/css"  rel="stylesheet" href="/static/css/bootstrap.min.css"/> 
  5.         <link  type="text/css"  rel="stylesheet" href="/static/css/bootstrap-responsive.css"/> 

 

不知道对大家有没有用,可做参考。

 

本文出自 “YEELONⒼ ” 博客,请务必保留此出处http://yeelone.blog.51cto.com/1476571/863583

 

 

 

发现通过以上方法还是不能正常显示,又找了网上的方法,最后发现还需要把模板里的image路径替换为动态的{{STATIC_URL}}才行。

既:<imgsrc="{{ STATIC_URL }}images/yourPic.jpg"/>

posted @ 2013-01-11 11:22  辛木  阅读(197)  评论(0)    收藏  举报