Login

Tag "imagefield"

Snippet List

Clear FileField/ImageField files in the Admin

The widget for FileField and ImageField has a problem: it doesn't supports clear its value and it doesn't delete the old file when you replace it for a new one. This is a solution for this. It is just for Admin, but you can make changes to be compatible with common forms. The jQuery code will put an **<input type="checkbox">** tag next to every **<input type="file">** and user can check it to clear the field value. When a user just replace the current file for a new one, the old file will be deleted.

  • admin
  • jquery
  • imagefield
  • filefield
Read More

ImageField for admin with thumbnail

AdminImageWidget is a ImageField Widget for admin that shows a thumbnail. Usage example on a form: class IconForm(forms.ModelForm): icon = forms.ImageField(label='icon', widget=AdminImageWidget)

  • image
  • admin
  • thumbnail
  • imagefield
  • widget
  • imagewidget
Read More

Use the primary key in FileField and ImageField filenames

Sometimes it is desirable to use values like the primary key when naming `FileField` and `ImageField` files, but such values are only available after saving the model instance. This abstract class implements a two-phase save in order to make this case easy. See the example in the docstring. Another solution would be to write a `save()` that requires `upload_to` to be a callable that checks for `instance.pk`, then calls it again after saving. However, this would require more work from the developer for simple cases.

  • imagefield
  • filefield
  • rename
  • upload_to
Read More

resize to thumbnail with scale-to-fill

**So you can upload rectangular pictures but still have square thumbnails.** "Scale to **fill**" instead of the out of the box "scale to **fit**" you get with `Image.thumbnail`

  • image
  • pil
  • thumbnail
  • resize
  • imagefield
Read More

Easy file upload handler

This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.

  • image
  • forms
  • view
  • upload
  • imagefield
  • filefield
  • file
Read More

Imagefield with variations

Usage: from django.db import models from imagevariations.fields import ImageVariationsField, Thumbnail class Image(models.Model): name = models.CharField(max_length=20) image = ImageVariationsField(upload_to='testimages', variations=(Thumbnail,) ) How to use in templates: Use the lowercase name of the image variation class. {{ object.image.variations.thumbnail.url }} By default all image variations will use the same storage backend as the field but can be replaced per variation by setting self.storage on the variation class.

  • thumbnail
  • imagefield
  • variations
Read More

CustomImageField for Django 1.0 alpha

The venerable CustomImageField, invented by [Scott Barnham](http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/) and rejiggered for newforms-admin by [jamstooks](http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/#comments). This here is a stab at a [post-Signals-refactor](http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring) version. Seems to do 'er. Note: This should be pointless once [fs-refactor](http://code.djangoproject.com/ticket/5361) lands.

  • models
  • fields
  • imagefield
  • newforms-admin
  • signals
Read More

Updated FileField / ImageField with a delete checkbox

Example model: class MyModel(models.Model): file = RemovableFileField(upload_to='files', \ null=True, blank=True) image = RemovableImageField(upload_to='images', \ null=True, blank=True) A delete checkbox will be automatically rendered when using ModelForm or editing it using form_for_instance. Also, the filename or the image will be displayed below the form field. You can edit the render method of the DeleteCheckboxWidget or add one to RemovableFileFormWidget to customize the rendering. UPDATE: 5. April 2009. Making it work with latest Django (thanks for the comments).

  • newforms
  • models
  • imagefield
  • filefield
  • remove
  • delete
Read More

Dynamic thumbnail generator

This snippet creates thumbnails on-demand from a ImageField with any size using dynamics methods, like ``get_photo_80x80_url`` or ``get_photo_640x480_filename``, etc. It assumes you have an `ImageField` in your Model called `photo` and have this in your models.py: import re from os import path from PIL import Image GET_THUMB_PATTERN = re.compile(r'^get_photo_(\d+)x(\d+)_(url|filename)$') `models.py` example: import re from os import path from PIL import Image from django.db import models GET_THUMB_PATTERN = re.compile(r'^get_photo_(\d+)x(\d+)_(url|filename)$') class Photo(models.Model): photo = models.ImageField(upload_to='photos/%Y/%m/%d') <snippet here> Example usage: >>> photo = Photo(photo="/tmp/test.jpg") >>> photo.save() >>> photo.get_photo_80x80_url() u"http://media.example.net/photos/2008/02/26/test_80x80.jpg" >>> photo.get_photo_80x80_filename() u"/srv/media/photos/2008/02/26/test_80x80.jpg" >>> photo.get_photo_64x64_url() u"http://media.example.net/photos/2008/02/26/test_64x64.jpg" >>> photo.get_photo_64x64_filename() u"/srv/media/photos/2008/02/26/test_64x64.jpg"

  • image
  • thumbnail
  • model
  • imagefield
Read More

ImageField with per user folder

If you need to upload Image files into folders qualified with user name eg.: 'images/user1/2008/01/01' then you may use this snippet. In order to use it you have to install ThreadLocals middleware as described here: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Then just import UserImageField class at your models.py and specify 'upload_to' parameter with '%(user)s' in the path eg.: ` image3 = UserImageField(_('Image 3'), upload_to='flower_images/%(user)s/%Y/%m/%d', null=True, blank=True) `

  • imagefield
  • path
Read More

FileField / ImageField with a delete checkbox

Example model: class MyModel(models.Model): file = RemovableFileField(upload_to='files', \ null=True, blank=True) image = RemovableImageField(upload_to='images', \ null=True, blank=True) A delete checkbox will be automatically rendered when editing such a model using form_for_instance. [UPDATED version which works with ModelForms](http://www.djangosnippets.org/snippets/636/)

  • newforms
  • models
  • imagefield
  • filefield
  • remove
  • delete
Read More

multiple model form with image

You have two models, one of which also has an image field. You want to get the data into a form and edit it. This also requires showing the url of the image, if there is one, on the form. This is a complete working example. No big deal, but for newbies, a complete working example could be valuable.

  • newforms
  • imagefield
Read More

Admin list thumbnail

This code will add a thumbnail image to your Model's Admin list view. The code will also generate the thumb images, so the first view may be a little slow loading. This assumes you have an **ImageField** in your Model called **image**, and the field's **upload_to** directory has a subdirectory called **tiny**. You then must add **"thumb"** to your Model's Admin **list_display**. The thumbnail images are also linked to the full size view of the image. I found this **VERY** useful... hope someone else does as well.

  • admin
  • imagefield
Read More

Filter to resize a ImageField on demand

A filter to resize a ImageField on demand, a use case could be: ` <img src="object.get_image_url" alt="original image"> <img src="object.image|thumbnail" alt="image resized to default 200x200 format"> <img src="object.image|thumbnail:"200x300" alt="image resized to 200x300"> ` The filter is applied to a image field (not the image url get from *get_image_url* method of the model), supposing the image filename is "image.jpg", it checks if there is a file called "image_200x200.jpg" or "image_200x300.jpg" on the second case, if the file isn't there, it resizes the original image, finally it returns the proper url to the resized image. There is a **TODO**: the filter isn't checking if the original filename is newer than the cached resized image, it should check it and resize the image again in this case.

  • filter
  • models
  • thumbnail
  • resize
  • imagefield
Read More

30 snippets posted so far.