[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="/upload/{{ user.userinfo.license }}" target="_blank">查看</a>

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


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

作者: orangleliu  





版权声明:本文orangleliu(http://blog.csdn.net/orangleliu/)原创文章,文章转载申报。

原文地址:https://www.cnblogs.com/zfyouxi/p/4818972.html