久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python - Django 怎么自動同步某個文件夾內的圖片至數據庫?

瀏覽:108日期:2022-08-23 17:39:36

問題描述

接觸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()

但是會報錯python - Django 怎么自動同步某個文件夾內的圖片至數據庫?

問題解答

回答1:

需要配置crontab來執行定期任務。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 武汉市| 定边县| 泰宁县| 延寿县| 岱山县| 东莞市| 杭州市| 烟台市| 安溪县| 宜丰县| 昌乐县| 长宁区| 内江市| 长岛县| 黑河市| 岫岩| 鄂尔多斯市| 万年县| 新民市| 勃利县| 炎陵县| 亳州市| 徐水县| 普兰店市| 高安市| 邳州市| 陇川县| 祁门县| 科技| 涟源市| 台江县| 黄浦区| 镶黄旗| 孝昌县| 舞阳县| 得荣县| 天全县| 平塘县| 眉山市| 阳信县| 宿松县|