首页 > 代码库 > [Django1.6]The MEDIA_ROOT and STATIC_ROOT settings must different 解决

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

项目中有个图片上传的功能,当时为了简单就把上传路径跟静态文件的路径写成了相同的,在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=http://www.mamicode.com/"/upload/{{ user.userinfo.license }}" target="_blank">查看

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


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

作者: orangleliu  





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