Login

Tag "ImageField"

Snippet List

Human readable file names decorator

This is extremely simple decorator to add possibility to upload files with name specific in some object field. For example image with same name as object slug. Sample **model**: class Test(models.Model): image = models.ImageField(\ upload_to=upload_to_dest(path='pics/', \ human_readable_field='hrname')) hrname = models.CharField( \ max_length=128, \ blank=True, default='') Sample **form** for admin: FNAME_EXP = re.compile('^[A-Za-z0-9\-\_]+$') class TestAdminForm(forms.ModelForm): hrname = forms.RegexField( label="Human Readable File Name", \ regex=FNAME_EXP, \ help_text="""Allowed only latin alphabet (upper and lower cases), underscore and minus characters. PLEASE, DO NOT INCLUDE EXTENSION OF THE FILE. Sample: test-this-file""", \ required=False) class Meta: model = Test Sample *admin.py* for *Test* model: class TestAdmin(admin.ModelAdmin): form = TestAdminForm admin.site.register(Test, TesetAdmin)

  • django
  • ImageField
  • file uploads
  • file name
  • FileField
Read More

ResizeImageField

ResizeImageField ================ (extension of RemovableImageField) ================================= by Wim Feijen, Go2People. What does it do? ---------------- ResizeImageField is a replacement for django's ImageField. It has two major benefits: 1. Creation of thumbnails and scaled images. 1. Extends the image upload form and adds a preview and a checkbox to remove the existing image. It's easy to use: - Replace ImageField by ResizeImageField - No further changes are necessary Requirements: ------------- Working installation of PIL, the Python Imaging Library Usage ----- - add resize_image to your app - add resize_filters.py to your templatetags - in settings.py, set a PHOTO_DIR, f.e. photos/original - in models.py, add: - from settings import PHOTO_DIR - from resize_image import ResizeImageField - photo = ResizeImageField(upload_to=PHOTO_DIR, blank=True) Scaled images will be stored in 'photos/scaled', thumbnails will be stored in 'photos/thumb'. Access your images from your template. Add:: {% load resize_filters %} {{ address.photo.url|thumb }} or:: {{ address.photo.url|scaled }} Defaults ------- - Scaled images are max. 200x200 pixels by default - Thumbnails are 50x50 pixels. Override the default behaviour in settings.py Scaling is done by PIL's thumbnail function, transparency is conserved. Credits ------ This code is an adaptation from python snippet 636 by tomZ: "Updated Filefield / ImageField with a delete checkbox"

  • image
  • thumbnail
  • resize
  • scale
  • delete
  • ImageField
  • ResizeImageField
  • removable
Read More
Author: wim
  • 5
  • 5

2 snippets posted so far.