python - Django 怎么自动同步某个文件夹内的图片至数据库?
问题描述
接触Django时间不长,不是很懂Django。希望有大神帮助。
最近在做一个小项目,其中有一个步骤是:某个程序会不断地生成一些图片,存放在路径C:UsersadminPictures下,现在想让程序自动地、随时把这些图片同步到Django APP 的数据库,而不是通过Django 的 Admin面板手工上传图片,请问应该怎么做呢?
目前定义的models:
models.py
from __future__ import unicode_literalsfrom django.db import modelsfrom django.conf import settingsfrom django.utils.encoding import python_2_unicode_compatible# Create your models here.@python_2_unicode_compatibleclass Image(models.Model): title = models.CharField(max_length=250, blank=True) original = models.ImageField(upload_to=settings.IMAGE_PREFIX, default=’/tmp/none.jpg’)def __str__(self):return self.title
尝试着写了一个脚本:
import osfrom gallery.models import Image import djangodjango.setup()path = r'C:UsersadminPictures'for filename in os.listdir(path): filePath = os.path.join(path, filename) thisFile = open(filePath, ’rb’) new_img = Image(original=thisFile, title=filename) new_img.save()
但是会报错
问题解答
回答1:需要配置crontab来执行定期任务。
相关文章:
1. 实现bing搜索工具urlAPI提交2. mysql优化 - MySQL如何为配置表建立索引?3. 冒昧问一下,我这php代码哪里出错了???4. 关于mysql联合查询一对多的显示结果问题5. php传对应的id值为什么传不了啊有木有大神会的看我下方截图6. windows误人子弟啊7. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。8. MySQL主键冲突时的更新操作和替换操作在功能上有什么差别(如图)9. python - linux 下用wsgifunc 运行web.py该如何修改代码10. 如何用笔记本上的apache做微信开发的服务器
