Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving to Django ImageField
    primarykey
    data
    text
    <p>I'm using jcrop to crop and image that I would then like to save to an ImageField.</p> <p>I've successfully saved to an ImageField in other sections of my site with the code below but in this instance I get the following error message:</p> <pre><code>'NoneType' object has no attribute 'rfind' </code></pre> <p><strong>My View</strong></p> <pre><code>def archive_update_profile_photo(request, id, slug): photo_to_crop_from= Media.objects.get(id=2) form = CropForm() if request.method == 'POST': form = CropForm(request.POST) if form.is_valid(): x = form.cleaned_data['x'] y = form.cleaned_data['y'] w = form.cleaned_data['w'] h = form.cleaned_data['h'] print x, y, w, h selected_media= Media.objects.get(id=2) raw_image = Image.open(selected_media.diplay_photo) # crop here l = x t = y r = x + w b = y + h cropped_image = raw_image.crop((l, t, r, b)) # resize here profile_image = cropped_image.resize((150, 150), Image.ANTIALIAS) profile_image.show() legacy = Legacy.objects.get(id=id) #save the profile image temp_handle = StringIO() profile_image.save(temp_handle, 'jpeg') temp_handle.seek(0) #save profile image to imagefield suf = SimpleUploadedFile(os.path.split(legacy.legacy_profile_image.name)[-1].split('.')[0], temp_handle.read(), content_type='image/jpeg') legacy.legacy_profile_image.save('%s.jpg' % suf.name, suf, save=True) return HttpResponseRedirect(reverse('archive_legacy', args=(id,slug))) return render(request, 'archive_app/archive_update_profile_photo.html', {'form': form,'photo_to_crop_from':photo_to_crop_from}) </code></pre> <p><strong>My Model</strong></p> <pre><code>def get_legacy_profile_image_name(request, instance): return 'profile_images/%s__%s' % (str(time()).replace('.','-'), 'legacy_profile_image') class Legacy(models.Model): created_date = models.DateTimeField(default=datetime.now) legacy_profile_image = models.ImageField(upload_to=get_legacy_profile_image_name, blank=True, null=True) </code></pre> <p><strong>Trackback</strong></p> <pre><code>Environment: Request Method: POST Request URL: http://localhost:8000/archive/update/profile/image/5/Virginia-Meeks/ Django Version: 1.5.4 Python Version: 2.7.1 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.humanize', 'bootstrap_toolkit', 'longerusername', 'south', 'storages', 'boto', 'archive') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/Users/bbrooke/Code/yourlegacy/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 115. response = callback(request, *callback_args, **callback_kwargs) File "/Users/bbrooke/Code/yourlegacy/env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 25. return view_func(request, *args, **kwargs) File "/Users/bbrooke/Code/yourlegacy/archive/views.py" in archive_update_profile_photo 500. suf = SimpleUploadedFile(os.path.split(legacy.legacy_profile_image.name)[-1].split('.')[0], temp_handle.read(), content_type='image/jpeg') File "/Users/bbrooke/Code/yourlegacy/env/bin/../lib/python2.7/posixpath.py" in split 83. i = p.rfind('/') + 1 Exception Type: AttributeError at /archive/update/profile/image/5/Virginia-Meeks/ Exception Value: 'NoneType' object has no attribute 'rfind' </code></pre> <p>I really appreciate your the feedback and expertise</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload