Python django.forms.ImageField() Examples

The following are 30 code examples for showing how to use django.forms.ImageField(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: SchoolIdolAPI   Author: MagiCircles   File: forms.py    License: Apache License 2.0 6 votes vote down vote up
def save(self, commit=True, use_card_filenames=False):
        instance = super(TinyPngForm, self).save(commit=False)
        for field in self.fields.keys():
            if (hasattr(instance, field)
                and field in dir(self.Meta.model)
                and type(self.Meta.model._meta.get_field(field)) == models.models.ImageField):
                image = self.cleaned_data[field]
                if image and (isinstance(image, InMemoryUploadedFile) or isinstance(image, TemporaryUploadedFile)):
                    filename = image.name
                    _, extension = os.path.splitext(filename)
                    if extension.lower() == '.png':
                        image = shrinkImageFromData(image.read(), filename)
                    if use_card_filenames and field in models.cardsImagesToName:
                        image.name = models.cardsImagesToName[field]({
                            'id': instance.id,
                            'firstname': instance.idol.name.split(' ')[-1] if instance.idol and instance.idol.name else 'Unknown',
                        })
                    else:
                        image.name = randomString(32) + extension
                    setattr(instance, field, image)
        if commit:
            instance.save()
        return instance 
Example 2
Project: StormOnline   Author: stormsha   File: images.py    License: Apache License 2.0 5 votes vote down vote up
def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs 
Example 3
Project: StormOnline   Author: stormsha   File: images.py    License: Apache License 2.0 5 votes vote down vote up
def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media 
Example 4
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __set__(self, instance, value):
        previous_file = instance.__dict__.get(self.field.name)
        super(ImageFileDescriptor, self).__set__(instance, value)

        # To prevent recalculating image dimensions when we are instantiating
        # an object from the database (bug #11084), only update dimensions if
        # the field had a value before this assignment.  Since the default
        # value for FileField subclasses is an instance of field.attr_class,
        # previous_file will only be None when we are called from
        # Model.__init__().  The ImageField.update_dimension_fields method
        # hooked up to the post_init signal handles the Model.__init__() cases.
        # Assignment happening outside of Model.__init__() will trigger the
        # update right here.
        if previous_file is not None:
            self.field.update_dimension_fields(instance, force=True) 
Example 5
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, verbose_name=None, name=None, width_field=None,
            height_field=None, **kwargs):
        self.width_field, self.height_field = width_field, height_field
        super(ImageField, self).__init__(verbose_name, name, **kwargs) 
Example 6
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def check(self, **kwargs):
        errors = super(ImageField, self).check(**kwargs)
        errors.extend(self._check_image_library_installed())
        return errors 
Example 7
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _check_image_library_installed(self):
        try:
            from PIL import Image  # NOQA
        except ImportError:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
                          'or run command "pip install Pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return [] 
Example 8
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(ImageField, self).deconstruct()
        if self.width_field:
            kwargs['width_field'] = self.width_field
        if self.height_field:
            kwargs['height_field'] = self.height_field
        return name, path, args, kwargs 
Example 9
Project: GTDWeb   Author: lanbing510   File: files.py    License: GNU General Public License v2.0 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {'form_class': forms.ImageField}
        defaults.update(kwargs)
        return super(ImageField, self).formfield(**defaults) 
Example 10
Project: django-multiupload   Author: Chive   File: fields.py    License: MIT License 5 votes vote down vote up
def to_python(self, data):
        ret = []
        for item in data:
            i = forms.ImageField.to_python(self, item)
            if i:
                ret.append(i)
        return ret 
Example 11
Project: bioforum   Author: reBiocoder   File: files.py    License: MIT License 5 votes vote down vote up
def __set__(self, instance, value):
        previous_file = instance.__dict__.get(self.field.name)
        super().__set__(instance, value)

        # To prevent recalculating image dimensions when we are instantiating
        # an object from the database (bug #11084), only update dimensions if
        # the field had a value before this assignment.  Since the default
        # value for FileField subclasses is an instance of field.attr_class,
        # previous_file will only be None when we are called from
        # Model.__init__().  The ImageField.update_dimension_fields method
        # hooked up to the post_init signal handles the Model.__init__() cases.
        # Assignment happening outside of Model.__init__() will trigger the
        # update right here.
        if previous_file is not None:
            self.field.update_dimension_fields(instance, force=True) 
Example 12
Project: bioforum   Author: reBiocoder   File: files.py    License: MIT License 5 votes vote down vote up
def _check_image_library_installed(self):
        try:
            from PIL import Image  # NOQA
        except ImportError:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
                          'or run command "pip install Pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return [] 
Example 13
Project: bioforum   Author: reBiocoder   File: files.py    License: MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {'form_class': forms.ImageField}
        defaults.update(kwargs)
        return super().formfield(**defaults) 
Example 14
Project: weibo-analysis-system   Author: Superbsco   File: images.py    License: MIT License 5 votes vote down vote up
def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs 
Example 15
Project: weibo-analysis-system   Author: Superbsco   File: images.py    License: MIT License 5 votes vote down vote up
def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media 
Example 16
Project: myblog   Author: myminwang   File: images.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs 
Example 17
Project: myblog   Author: myminwang   File: images.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media 
Example 18
Project: CTF_AWD_Platform   Author: xuchaoa   File: images.py    License: MIT License 5 votes vote down vote up
def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs 
Example 19
Project: CTF_AWD_Platform   Author: xuchaoa   File: images.py    License: MIT License 5 votes vote down vote up
def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media 
Example 20
Project: django_OA   Author: abaoMAO   File: images.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs 
Example 21
Project: django_OA   Author: abaoMAO   File: images.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" data-gallery="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media 
Example 22
Project: django-croppie   Author: dima-kov   File: fields.py    License: MIT License 5 votes vote down vote up
def __init__(self, options={}, widget=None, *args, **kwargs):
        fields = (
            forms.ImageField(),
            forms.CharField(),
            forms.CharField(),
            forms.CharField(),
            forms.CharField(),
        )
        if widget is None:
            widget = CroppieImageRatioWidget(options=options)

        super(CroppieField, self).__init__(
            fields=fields, widget=widget, *args, **kwargs) 
Example 23
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: files.py    License: MIT License 5 votes vote down vote up
def __set__(self, instance, value):
        previous_file = instance.__dict__.get(self.field.name)
        super().__set__(instance, value)

        # To prevent recalculating image dimensions when we are instantiating
        # an object from the database (bug #11084), only update dimensions if
        # the field had a value before this assignment.  Since the default
        # value for FileField subclasses is an instance of field.attr_class,
        # previous_file will only be None when we are called from
        # Model.__init__().  The ImageField.update_dimension_fields method
        # hooked up to the post_init signal handles the Model.__init__() cases.
        # Assignment happening outside of Model.__init__() will trigger the
        # update right here.
        if previous_file is not None:
            self.field.update_dimension_fields(instance, force=True) 
Example 24
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: files.py    License: MIT License 5 votes vote down vote up
def _check_image_library_installed(self):
        try:
            from PIL import Image  # NOQA
        except ImportError:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.org/project/Pillow/ '
                          'or run command "pip install Pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return [] 
Example 25
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: files.py    License: MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        return super().formfield(**{
            'form_class': forms.ImageField,
            **kwargs,
        }) 
Example 26
Project: python   Author: Yeah-Kun   File: files.py    License: Apache License 2.0 5 votes vote down vote up
def __set__(self, instance, value):
        previous_file = instance.__dict__.get(self.field.name)
        super(ImageFileDescriptor, self).__set__(instance, value)

        # To prevent recalculating image dimensions when we are instantiating
        # an object from the database (bug #11084), only update dimensions if
        # the field had a value before this assignment.  Since the default
        # value for FileField subclasses is an instance of field.attr_class,
        # previous_file will only be None when we are called from
        # Model.__init__().  The ImageField.update_dimension_fields method
        # hooked up to the post_init signal handles the Model.__init__() cases.
        # Assignment happening outside of Model.__init__() will trigger the
        # update right here.
        if previous_file is not None:
            self.field.update_dimension_fields(instance, force=True) 
Example 27
Project: python   Author: Yeah-Kun   File: files.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
        self.width_field, self.height_field = width_field, height_field
        super(ImageField, self).__init__(verbose_name, name, **kwargs) 
Example 28
Project: python   Author: Yeah-Kun   File: files.py    License: Apache License 2.0 5 votes vote down vote up
def check(self, **kwargs):
        errors = super(ImageField, self).check(**kwargs)
        errors.extend(self._check_image_library_installed())
        return errors 
Example 29
Project: python   Author: Yeah-Kun   File: files.py    License: Apache License 2.0 5 votes vote down vote up
def _check_image_library_installed(self):
        try:
            from PIL import Image  # NOQA
        except ImportError:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
                          'or run command "pip install Pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return [] 
Example 30
Project: python   Author: Yeah-Kun   File: files.py    License: Apache License 2.0 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(ImageField, self).deconstruct()
        if self.width_field:
            kwargs['width_field'] = self.width_field
        if self.height_field:
            kwargs['height_field'] = self.height_field
        return name, path, args, kwargs