本文整理汇总了Python中django.forms.ImageField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.ImageField方法的具体用法?Python forms.ImageField怎么用?Python forms.ImageField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms
的用法示例。
在下文中一共展示了forms.ImageField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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
示例2: get_field_attrs
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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
示例3: get_field_result
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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
示例4: __set__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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)
示例5: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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)
示例6: check
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
def check(self, **kwargs):
errors = super(ImageField, self).check(**kwargs)
errors.extend(self._check_image_library_installed())
return errors
示例7: _check_image_library_installed
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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 []
示例8: deconstruct
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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
示例9: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
def formfield(self, **kwargs):
defaults = {'form_class': forms.ImageField}
defaults.update(kwargs)
return super(ImageField, self).formfield(**defaults)
示例10: to_python
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
def to_python(self, data):
ret = []
for item in data:
i = forms.ImageField.to_python(self, item)
if i:
ret.append(i)
return ret
示例11: __set__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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)
示例12: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
def formfield(self, **kwargs):
defaults = {'form_class': forms.ImageField}
defaults.update(kwargs)
return super().formfield(**defaults)
示例13: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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)
示例14: _check_image_library_installed
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
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 []
示例15: formfield
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ImageField [as 别名]
def formfield(self, **kwargs):
return super().formfield(**{
'form_class': forms.ImageField,
**kwargs,
})