码迷,mamicode.com
首页 > 其他好文 > 详细

[Django1.6]The MEDIA_ROOT and STATIC_ROOT settings must different 解决

时间:2014-10-28 17:54:48      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:django   上传   路径   media_root   static_root   

项目中有个图片上传的功能,当时为了简单就把上传路径跟静态文件的路径写成了相同的,在wi7的机器上运行没有问题,今天在centos的机器上就报了如下的错误:

django.core.exceptions.ImproperlyConfigured: The MEDIA_ROOT and STATIC_ROOT settings must have different values


google下,直接就到了django的文档中:

MEDIA_ROOT and STATIC_ROOT must have different values. Before STATIC_ROOT was introduced, it was common to rely or fallback on MEDIA_ROOT to also serve static files; however, since this can have serious security implications, there is a validation check to prevent it.


文档中明确的指出MEDIA_ROOT 跟 STATIC_ROOT不能用同一个路径。在STATIC_ROOT么有引进之前使用MEDIA_ROOT路径来存储静态文件的(css,js之类),为了安全。现在基本是使用MEDIA_ROOT 来存储上传文件,STATIC_ROOT来存储静态文件。 


解决“:

settings中修改: 

MEDIA_ROOT 定义为别的路径

MEDIA_URL = '/upload/'
MEDIA_ROOT = os.path.join(BASE_DIR,'uploadfiles').replace('\\','/')

然后在全局的urls配置用添加

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

最后在页面显示图片的地方修改下连接地址就可以了。

<a href="/upload/{{ user.userinfo.license }}" target="_blank">查看</a>

基本上就这个三个步骤,就能替换原来的存储路径了。


本文出自 “orangleliu笔记本” 博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/40542099

作者: orangleliu  





[Django1.6]The MEDIA_ROOT and STATIC_ROOT settings must different 解决

标签:django   上传   路径   media_root   static_root   

原文地址:http://blog.csdn.net/orangleliu/article/details/40542099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!