[Django]静态文件:如何在网页中显示图片,css,js等静态文件

翻译整理自Django的官网文档。
具体步骤为:
  1.在settings.py中添加与修改:
  1. # static/file operation  
  2. HERE = os.path.dirname(os.path.dirname(__file__))  
  3.   
  4. # Absolute filesystem path to the directory that will hold user-uploaded files.  
  5. # Example: "/home/media/media.lawrence.com/media/"  
  6. #MEDIA_ROOT = ''  
  7. MEDIA_ROOT = os.path.join( HERE , 'media/').replace('\\','/')  
  8. # URL that handles the media served from MEDIA_ROOT. Make sure to use a  
  9. # trailing slash.  
  10. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"  
  11. #MEDIA_URL = ''  
  12. MEDIA_URL = '/media/'  
  13.   
  14. # Absolute path to the directory static files should be collected to.  
  15. # Don't put anything in this directory yourself; store your static files  
  16. # in apps' "static/" subdirectories and in STATICFILES_DIRS.  
  17. # Example: "/home/media/media.lawrence.com/static/"  
  18. STATIC_ROOT = ''  
  19. STATIC_PATH = os.path.join( HERE , 'static').replace('\\','/')  
  20.   
  21. # URL prefix for static files.  
  22. # Example: "http://media.lawrence.com/static/"  
  23. STATIC_URL = '/static/'  
  24. # add property : ADMIN_MEDIA_ROOT BY slider  
  25. # ADMIN_MEDIA_ROOT = '/static/admin/'  
  26. # Additional locations of static files  
  27. STATICFILES_DIRS = (  
  28.     # Put strings here, like "/home/html/static" or "C:/www/django/static".  
  29.     # Always use forward slashes, even on Windows.  
  30.     # Don't forget to use absolute paths, not relative paths.  
  31.     os.path.join(HERE,'static').replace('\\','/'),  
  32. )  

项目urls.py里的代码:
  1. from django.conf import settings  
  2. if settings.DEBUG:  
  3.     urlpatterns += patterns('',  
  4.             (r'^static/(?P<path>.*)$''django.views.static.serve', {'document_root': settings.STATIC_PATH, 'show_indexes':True}),  
  5.             )  


然后在模板里面这样引用(这里css文件的目录是 : 项目根目录/static/forum/csss/base.css):

  1. <link rel="stylesheet" type="text/css" href="/static/forum/css/base.css"/>  


MEDIA_ROOT 用于存放user上传的东西
STATIC_ROOT用于存放网站自己的js,css,图片
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值