databaseからimageへのpathを得て、ディレクトリ内のimageを開いているつもりなんですが上手く行きません。
objectとして得たものをどうしたらimage fileとして扱えるようになるのでしょうか。
発生している問題・エラーメッセージ
/anaconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1447: RuntimeWarning: DateTimeField MainImageInfo.data received a naive datetime (2018-03-3022:50:26.966149) while time zone support is active. RuntimeWarning) Internal Server Error: /mosaic_app/get_album_images/ Traceback (most recent call last): File "/anaconda3/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/Yoshinori/Desktop/mosaic/test4/myProject/mosaic_app/views.py", line 183, in get_album_images image = Image.open(path) File "/anaconda3/lib/python3.6/site-packages/PIL/Image.py", line 2481, in open fp.seek(0) File "/anaconda3/lib/python3.6/site-packages/django/core/files/utils.py", line 20, in <lambda> seek = property(lambda self: self.file.seek) File "/anaconda3/lib/python3.6/site-packages/django/db/models/fields/files.py", line 50, in _get_file self._require_file() File "/anaconda3/lib/python3.6/site-packages/django/db/models/fields/files.py", line 47, in _require_file raise ValueError("The '%s' attribute has no file associated with it." % self.field.name) ValueError: The 'main_image' attribute has no file associated with it.
該当のソースコード
views.py
python
1from PIL import Image 2from .models import MainImageInfo 3 4 5main_image_path = MainImageInfo.objects.order_by("main_image")[0] 6path = main_image_path.main_image 7image = Image.open(path) 8
models.py
python
1 2 3class MainImageInfo(models.Model): 4 5 main_image = models.ImageField(upload_to ="main_images",blank=True) 6 data = models.DateTimeField(default=datetime.now) 7
試したこと
print(type(main_image_path)) >> <class 'mosaic_app.models.MainImageInfo'> print(main_image_path) >> MainImageInfo object print(type(path)) >> <class 'django.db.models.fields.files.ImageFieldFile'> print(path) >> [30/Mar/2018 23:04:22] "POST /mosaic_app/get_album_images/ HTTP/1.1" 200 3867
属性が違うから、扱えないということなのでしょうか。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。