本文整理匯總了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,
})