本文整理汇总了Python中django.forms.ClearableFileInput方法的典型用法代码示例。如果您正苦于以下问题:Python forms.ClearableFileInput方法的具体用法?Python forms.ClearableFileInput怎么用?Python forms.ClearableFileInput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms
的用法示例。
在下文中一共展示了forms.ClearableFileInput方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def render(self, name, value, attrs=None, renderer=None):
name = str(name)
substitutions = {
'initial_text': self.initial_text,
'input_text': self.input_text,
'clear_template': '',
'clear_checkbox_label': self.clear_checkbox_label,
}
template = '%(input)s'
substitutions['input'] = super(forms.ClearableFileInput, self).render(name, value, attrs, renderer=renderer)
if value and hasattr(value, "url"):
template = self.template_with_initial
substitutions['initial'] = ('<a href="%s">%s</a>'
% (escape(value.file_sub.get_file_url()),
escape(value.file_sub.display_filename())))
if not self.is_required:
checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name)
substitutions['clear_checkbox_name'] = escape(checkbox_name)
substitutions['clear_checkbox_id'] = escape(checkbox_id)
substitutions['clear'] = forms.CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
substitutions['clear_template'] = self.template_with_clear % substitutions
return mark_safe(template % substitutions)
示例2: get_field
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def get_field(param, initial=None):
field = param.form_field
choices = json.loads(param.choices)
field_kwargs = {'label': param.script_param.title(),
'required': param.required,
'help_text': param.param_help,
}
if choices:
field = 'ChoiceField'
base_choices = [(None, '----')] if not param.required else []
field_kwargs['choices'] = base_choices+[(str(i), str(i).title()) for i in choices]
if field == 'FileField':
if param.is_output:
field = 'CharField'
initial = None
else:
if initial is not None:
initial = utils.get_storage_object(initial) if not hasattr(initial, 'path') else initial
field_kwargs['widget'] = forms.ClearableFileInput()
if initial is not None:
field_kwargs['initial'] = initial
field = getattr(forms, field)
return field(**field_kwargs)
示例3: test_accept
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def test_accept(self):
widget = ClearableFileInput()
assert 'accept' not in widget.render(name='file', value='test.jpg')
assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(None)
widget = ClearableFileInput(attrs={'accept': 'image/*'})
assert 'accept="image/*"' in widget.render(name='file', value='test.jpg')
assert [
"starts-with", "$Content-Type", "image/"
] in widget.get_conditions('image/*')
widget = ClearableFileInput(attrs={'accept': 'image/jpeg'})
assert 'accept="image/jpeg"' in widget.render(name='file', value='test.jpg')
assert {"Content-Type": 'image/jpeg'} in widget.get_conditions('image/jpeg')
widget = ClearableFileInput(attrs={'accept': 'application/pdf,image/*'})
assert 'accept="application/pdf,image/*"' in widget.render(
name='file',
value='test.jpg',
)
assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(
'application/pdf,image/*')
assert {"Content-Type": 'application/pdf'} not in widget.get_conditions(
'application/pdf,image/*')
示例4: test_build_attr
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def test_build_attr(self):
assert set(ClearableFileInput().build_attrs({}).keys()) == {
'class',
'data-url',
'data-fields-x-amz-algorithm',
'data-fields-x-amz-date',
'data-fields-x-amz-signature',
'data-fields-x-amz-credential',
'data-fields-policy',
'data-fields-key',
}
assert ClearableFileInput().build_attrs({})['class'] == 's3file'
assert ClearableFileInput().build_attrs(
{'class': 'my-class'})['class'] == 'my-class s3file'
示例5: test_get_conditions
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def test_get_conditions(self, freeze):
conditions = ClearableFileInput().get_conditions(None)
assert all(condition in conditions for condition in [
{"bucket": 'test-bucket'},
{"success_action_status": "201"},
['starts-with', '$key', 'custom/location/tmp'],
["starts-with", "$Content-Type", ""]
]), conditions
示例6: test_media
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def test_media(self):
assert ClearableFileInput().media._js == ['s3file/js/s3file.js']
示例7: test_upload_folder
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def test_upload_folder(self):
assert ClearableFileInput().upload_folder.startswith(
'custom/location/tmp/s3file/'
)
assert len(ClearableFileInput().upload_folder) == 49
示例8: is_file
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def is_file(field):
return isinstance(field.field.widget, forms.ClearableFileInput)
示例9: prepare_file
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ClearableFileInput [as 别名]
def prepare_file(self, field):
field_attrs = field.build_field_attrs()
widget_attrs = field.build_widget_attrs()
field_attrs.update({
'widget': forms.ClearableFileInput(attrs=widget_attrs)
})
if field.choice_values:
regex = re.compile('[\s]*\n[\s]*')
choices = regex.split(field.choice_values)
field_attrs.update({
'allowed_file_types': [i.lstrip('.').lower() for i in choices]
})
return FormBuilderFileField(**field_attrs)