Django中配置静态文件路径

选择了最近一直在看的Django作为我的Android Demo的server端,今天想要实现一个Android下上传文件到服务器的功能,于是想了想,暂时可以先放到静态文件目录下,然后就把自己配置静态文件目录的过程记录一下。

首先打开你的项目下的settings.py文件,在其中加入或修改如下代码:

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # 设置一个路径变量  
  2. APP_PATH=os.path.dirname(os.path.dirname(__file__))  
  3. # Absolute path to the directory static files should be collected to.  
  4. # Don't put anything in this directory yourself; store your static files  
  5. # in apps' "static/" subdirectories and in STATICFILES_DIRS.  
  6. # 注意要修改STATIC_ROOT变量  
  7. STATIC_ROOT = os.path.join(APP_PATH,'static').replace('\\','/')  
  8.   
  9. # URL prefix for static files.  
  10. # Example: "http://example.com/static/", "http://static.example.com/"  
  11. STATIC_URL = '/static/'  
  12.   
  13. # 当然还有STATICFILES_DIRS变量  
  14. STATICFILES_DIRS = (  
  15.     # Put strings here, like "/home/html/static" or "C:/www/django/static".  
  16.     # Always use forward slashes, even on Windows.  
  17.     # Don't forget to use absolute paths, not relative paths.  
  18.     os.path.join(APP_PATH,'mobile_oa_server/static').replace('\\','/'),  
  19. )  
然后在你的项目下的urls.py文件中加入如下代码:

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # 导入static和settings  
  2. from django.conf.urls.static import static  
  3. from django.conf import settings  
  4.   
  5. urlpatterns = patterns('',  
  6.     # 这里是你原先的urlpatterns的值,记住一定要在urlpatterns的下方,追加static  
  7. )  
  8. urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)  

OK,完成了上述步骤之后,我们可以在我们的app目录下创建一个static文件夹,然后扔一张照片啥的进去,为了检测是否成功:效果如下(随便找了张图作测试)


夜已深,收拾完这个小东西就准备睡觉去了,明天又是要奋斗的一天,大家加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值