静态文件路径设置官方说明

1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

2. In your settings file, define STATIC_URL, for example:   

STATIC_URL = '/static/'

3. Store your static files in a folder called static in your app. For example my_app/static/my_app/myimage.jpg.

4. In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).

{% load staticfiles %}

<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>

使用实例

在前面的例子中,我们使用了上面第三种设置方式,在app目录下,创建了static目录,里面还创建了名字跟app名称一样的目录,里面才放静态资源文件,路径如:

polls/static/polls/style.css,在template中引入时使用

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

一、Django 项目默认读取静态文件目录如 /projectName/appName/static/

二、如果我们不想将静态文件放到app甚至project目录下时,可以在settings.py 中设置 STATICFILES_DIRS ,如:

STATICFILES_DIRS = (
'D://static/',
# os.path.join(BASE_DIR, "static"),
# '/var/www/static/',
)

接着前面的例子,此时创建 D://static/polls/style.css

li a {
color: red;
} body {
background: white url("images/background.jpg") no-repeat center Top;
}

刷新页面 则可发现,链接中的文字颜色由原先的绿色变成了红色,说明页面引用的css路径生效


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

最新文章

  1. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World
  2. 关于Linux 下 Mysql 远程访问时出现的Access denied for user &#39;用户名&#39;@&#39;IP地址&#39; (using password:NO)
  3. restController与Controller-待续
  4. Flume1.5.0的安装、部署、简单应用(含伪分布式、与hadoop2.2.0、hbase0.96的案例)
  5. Android判断当前线程是否是主线程的方法
  6. [BZOJ 1052][HAOI2007]覆盖问题(二分答案)
  7. Android 上下文菜单实现
  8. Cocos2d-x内存管理研究&lt;二&gt;
  9. C++学习(五)
  10. Android从无知到有知——NO.4
  11. 使用ReSharper打造团队代码
  12. 传统的MVC模式
  13. zeromq随笔
  14. Python的hasattr() getattr() setattr() 函数使用方法详解 (转)
  15. android使用百度地图最新sdk5.0后后代码混淆时,地图无法显示闪退问题
  16. dnSpy 强大的.Net反编译软件
  17. Python量化交易
  18. 第43章:MongoDB-集群--Sharding(分片)--多机的搭建
  19. js Ajax 请求返回
  20. iptables的增删改查

热门文章

  1. kattle 发送post请求
  2. appdata
  3. 002.php安装(lnmp)
  4. JS中误用/g导致的正则test()无法正确重复执行
  5. CSS/CSS3常用样式小结
  6. java怎么导入一个项目到eclipse
  7. 俄罗斯方块(Java实现)
  8. libpng使用
  9. Linux进程管理子系统分析【转】
  10. p2p网贷系统即将上线