richardbarran / django-photologue

A customizable plug-in photo gallery management application for the Django web framework.
BSD 3-Clause "New" or "Revised" License
676 stars 238 forks source link

How to programmatically save photos? #167

Closed jandetlefsen closed 7 years ago

jandetlefsen commented 7 years ago

How can i programmatically create a photo with photologue. This is what i tried but i'm getting an error.

from django.core.files.base import ContentFile
import urllib2
from photologue.models import Photo

file = urllib2.urlopen('http://placehold.it/350x150')
photo = Photo(title='Photo Title', slug='photo-title', image=ContentFile(file))
photo.save()

Error i'm seeing:

No handlers could be found for logger "photologue.models"
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 555, in save
    super(Photo, self).save(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 506, in save
    self.pre_cache()
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 474, in pre_cache
    self.create_size(photosize)
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 413, in create_size
    if self.size_exists(photosize):
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 366, in size_exists
    if self.image.storage.exists(func()):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 13, in _curried
    return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 344, in _get_SIZE_filename
    return smart_str(os.path.join(self.cache_path(),
  File "/usr/local/lib/python2.7/dist-packages/photologue/models.py", line 309, in cache_path
    return os.path.join(os.path.dirname(self.image.name), "cache")
  File "/usr/lib/python2.7/posixpath.py", line 129, in dirname
    i = p.rfind('/') + 1
AttributeError: 'NoneType' object has no attribute 'rfind'
richardbarran commented 7 years ago

Hi, I don't think that you can attach an image file when you create a model instance - the way I usually do it is to create the model instance & then save the image file to the instance. See here for an example. But I'm happy to be proven wrong if someone suggests a quicker route!

jandetlefsen commented 7 years ago

thank you that did the trick.