apache静态文件配置
来源:互联网 发布:星际淘宝网588 编辑:程序博客网 时间:2024/06/08 15:25
开发环境配置
需要下面几个步骤
1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等
2. 确保settings.py中的INSTALLED_APPS中包含django.contrib.staticfiles
3. 设置settings.py中的STATIC_URL的值为“/static/”
4. 在模版中使用{{ STATIC_URL }}作为静态文件路径前缀。比如纯在图片文件your_app/static/img/logo.png,那么应用代码为<img src=”{{ STATIC_URL }}img/logo.png”>
5. 渲染模版的Context对象需要换成RequestContext,否则模版中无法引用到STATIC_URL对应的值。
上面的操作步骤参见官方文档:https://docs.djangoproject.com/en/dev/howto/static-files/
特别注意:按照上述设置,settings.py中的DEBUG选项必须设置为True,否则'/static/'无法映射到静态文件目录。参见这里:http://stackoverflow.com/a/4566907/1114397
生产环境配置
我的发布环境是apache,这里只介绍相关配置。
使用apache的静态发布配置,而不使用django的配置,这也是django推荐的方式,因为django认为静态文件配置的方法交给服务器,会更有效。你所需要做的就是在apahce的配置文件中添加一个alice,如下
Alias /static "/your_wsgi/your_app/static"
# 注意结尾都没有斜杠,全路径也没有。
# 如果alias后面的值有反斜杠结尾,那么全路径也需要有,保持一致
“/static”对应django settings.py中配置的STATIC_URL的值。然后添加directory选项,配置相关目录的权限。Apache 2.4版本开始,对directory配置做出了调整,所以这里需要注意,如下:
Apache v2.4及以上版本,
<Directory "/your_wsgi/your_app/static ">
Require all granted
</Directory>
Apache v2.4以下版本
<Directory "/your_wsgi/your_app/static ">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
注意上面红色部分。
Tips:如果访问静态图片遇到403错误,此时说明路径配置成功,离成功不远了。只是路径权限没有配置正确,这时可以看看apahce错误日志,如果出现“client denied by server configuration”,说明是directory的权限没有配置正确,可以查看自己的apache版本,然后做出响应配置。
希望本文对你有用!
- apache静态文件配置
- apache配置静态文件缓存
- Apache配置伪静态
- apache 拟静态配置
- apache 伪静态配置
- apache配置伪静态
- apache伪静态配置
- apache静态服务器配置
- Apache-伪静态配置
- Apache SSI配置 动态include静态html文件
- apache 下配置gzip 和静态文件缓存
- Apache SSI配置 动态include静态html文件
- apache配置伪静态隐藏index.php文件
- Apache静态文件访问配置(书封服务器)
- Apache Rewrite 拟静态配置
- Apache Rewrite 拟静态配置
- Apache Rewrite 拟静态配置
- Apache + Tomcat 配置伪静态
- Android开发——倒计时器自定义chronometer
- Spring 配置多数据源实现
- leetcode--Power of Two
- 李飞飞-我们该如何做研究
- 【NOIP提高组】B
- apache静态文件配置
- A【NOIP2017提高组模拟12.18】
- 对pearson相关系数以及协方差矩阵的理解
- 泰勒与极限
- MFC操作注册表
- c++教程(一)c过渡c++
- 图的强联通分量模板
- jquery.form实现文件上传
- UX入門 - 1