选择了最近一直在看的Django作为我的Android Demo的server端,今天想要实现一个Android下上传文件到服务器的功能,于是想了想,暂时可以先放到静态文件目录下,然后就把自己配置静态文件目录的过程记录一下。
首先打开你的项目下的settings.py文件,在其中加入或修改如下代码:
# 设置一个路径变量 APP_PATH=os.path.dirname(os.path.dirname(__file__)) # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # 注意要修改STATIC_ROOT变量 STATIC_ROOT = os.path.join(APP_PATH,'static').replace('\\','/') # URL prefix for static files. # Example: "http://example.com/static/", "http://static.example.com/" STATIC_URL = '/static/' # 当然还有STATICFILES_DIRS变量 STATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. os.path.join(APP_PATH,'mobile_oa_server/static').replace('\\','/'), )
然后在你的项目下的urls.py文件中加入如下代码:
# 导入static和settings from django.conf.urls.static import static from django.conf import settings urlpatterns = patterns('', # 这里是你原先的urlpatterns的值,记住一定要在urlpatterns的下方,追加static ) urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
OK,完成了上述步骤之后,我们可以在我们的app目录下创建一个static文件夹,然后扔一张照片啥的进去,为了检测是否成功:效果如下(随便找了张图作测试)
夜已深,收拾完这个小东西就准备睡觉去了,明天又是要奋斗的一天,大家加油!
2013年11月17日,Eric.Tang 记