jdriscoll / django-imagekit

Automates image processing for Django models. Resize, process and cache multiple versions of your image files. Access newly created files with a standard API. Supports alternate storage schemes such as Amazon S3.

Clone this repository (size: 135.6 KB): HTTPS / SSH
$ hg clone http://hg.driscolldev.com/django-imagekit
follow

In the Django admin interface I have an ImageField that isn't required. When I save the record I'm working on without an image I get the following trackback. I've attached my quick and dirty fix, which seems to work fine.

#!Traceback (most recent call last):

  File "/home/code/django/1.1.X/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/code/django/1.1.X/django/contrib/admin/sites.py", line 490, in root
    return self.model_page(request, *url.split('/', 2))

  File "/home/code/django/1.1.X/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)

  File "/home/code/django/1.1.X/django/contrib/admin/sites.py", line 509, in model_page
    return admin_obj(request, rest_of_url)

  File "/home/code/django/1.1.X/django/contrib/admin/options.py", line 1098, in __call__
    return self.change_view(request, unquote(url))

  File "/home/code/django/1.1.X/django/db/transaction.py", line 240, in _commit_on_success
    res = func(*args, **kw)

  File "/home/code/django/1.1.X/django/contrib/admin/options.py", line 830, in change_view
    self.save_model(request, new_object, form, change=True)

  File "/home/code/django/1.1.X/django/contrib/admin/options.py", line 557, in save_model
    obj.save()

  File "/usr/lib/python2.5/site-packages/django_imagekit-0.3.2-py2.5.egg/imagekit/models.py", line 144, in save
    self._clear_cache()

  File "/usr/lib/python2.5/site-packages/django_imagekit-0.3.2-py2.5.egg/imagekit/models.py", line 104, in _clear_cache
    prop._delete()

  File "/usr/lib/python2.5/site-packages/django_imagekit-0.3.2-py2.5.egg/imagekit/specs.py", line 69, in _delete
    self._obj._storage.delete(self.name)

  File "/usr/lib/python2.5/site-packages/django_imagekit-0.3.2-py2.5.egg/imagekit/specs.py", line 76, in name
    filepath, basename = os.path.split(self._obj._imgfield.name)

  File "/usr/lib/python2.5/posixpath.py", line 77, in split
    i = p.rfind('/') + 1

AttributeError: 'NoneType' object has no attribute 'rfind'

Status: new Responsible: Justin Driscoll / jdriscoll Type: bug
Milestone: none Component: none Version: none

Attachments

Comments and changes

#1

Justin Driscoll / jdriscoll

Do you have "null=True" for the ImageField?

You're getting the exception because _clear_cache is being called but the following check is made to see if the imagefield is populated

1
2
3
#line 143 in model.py
if clear_cache and self._imgfield != '':
            self._clear_cache()

The first guess would be this check is failing because the field is null and not an empty string. I almost never null out fields so I've never run into this. I should probably update the check to look for a null value as well anyway.


#2 Anonymous

written 4 months ago, last edited unknown.

Using implicit false evaluation as suggested in Google's style guide [1] would work for both empty strings and nullable fields.

1
2
if clear_cache and not self._imgfield:
    self._clear_cache()

[1] http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=True/False_evaluations#True/False_evaluations


#3

Eddie Welker / edwelker

Bump!


#4 Anonymous

written 3 months ago, last edited unknown.

can you explain this solution better? because i have this problem but i don't understand where you put this code for solve this problem.

thanks.


#5

Justin Driscoll / jdriscoll

Just change line 143 in models.py to look like the above. I'll get this fixed. Sorry about the delay.


#6

Justin Driscoll / jdriscoll

I've updated the code. Let me know if this fixes your issue.


#7 Anonymous

written 3 months ago, last edited unknown.

As a follow up, I put default=''into my model definition which also seemed to fix the bug (seems like the pypi package hasn't been updated yet so if your installing from pip or easy_install this may help you out).

media_file = models.ImageField(blank=True, null=True, default='', upload_to='path/to/dir')

Thanks for a great package btw :)

Chris Reeves


#8 Anonymous

written 2 months ago, last edited unknown.

Updated to the new code, but now when I was testing for a url,

if model.display.url

it's now returning non-null, model_imgs/_display.

I don't think the default='' is a particularly good solution


#9

Simon Zimmermann / simonz05

I was still getting the error:

Fixed it by adding a check to the last line in the save() method in models.py. I have no idea if this is a good or bad fix, but it seems to work out fine. Would be nice to have it verified, though.

1
2
if self._imgfield:
            self._pre_cache()

#10

adamnelson

Issue #16 was marked as a duplicate of this issue.


#11

adamnelson

Issue #22 was marked as a duplicate of this issue.


Add comment / attachment

Show/hide preview

Verification: Please write the text from the image in the box (letters only)

captcha

Is that you, Humanoid? Is this me?