當前位置: 首頁>>代碼示例>>Python>>正文


Python forms.FileField方法代碼示例

本文整理匯總了Python中django.forms.FileField方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.FileField方法的具體用法?Python forms.FileField怎麽用?Python forms.FileField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.forms的用法示例。


在下文中一共展示了forms.FileField方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: field_value

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def field_value(field):
	""" 
	Returns the value for this BoundField, as rendered in widgets. 
	""" 
	if field.form.is_bound: 
		if isinstance(field.field, FileField) and field.data is None: 
			val = field.form.initial.get(field.name, field.field.initial) 
		else: 
			val = field.data 
	else:
		val = field.form.initial.get(field.name, field.field.initial)
		if isinstance(val, collections.Callable):
			val = val()
	if val is None:
		val = ''
	return val 
開發者ID:sfu-fas,項目名稱:coursys,代碼行數:18,代碼來源:submission_filters.py

示例2: clean

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def clean(self, value, initial):
        files = value['files']
        cleared = value['cleared']
        if not files and not cleared:
            return initial
        new = [
            FileField(validators=[
                FileExtensionValidator(allowed_extensions=settings.FILE_ALLOWED_EXTENSIONS)
            ]).clean(file, initial) for file in files
        ]

        if initial:
            old = [file for i, file in enumerate(initial) if i not in cleared]
        else:
            old = []

        return old + new 
開發者ID:OpenTechFund,項目名稱:hypha,代碼行數:19,代碼來源:fields.py

示例3: process_form_submission

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def process_form_submission(self, form):
        cleaned_data = form.cleaned_data

        for name, field in form.fields.items():
            if isinstance(field, FileField):
                file_data = cleaned_data[name]
                if file_data:
                    file_name = file_data.name
                    file_name = webform_storage.generate_filename(file_name)
                    upload_to = os.path.join('webform', str(self.id), file_name)
                    saved_file_name = webform_storage.save(upload_to, file_data)
                    file_details_dict = {name: webform_storage.url(saved_file_name)}
                    cleaned_data.update(file_details_dict)
                else:
                    del cleaned_data[name]

        form_data = json.dumps(cleaned_data, cls=DjangoJSONEncoder)
        return self.get_submission_class().objects.create(
            form_data=form_data,
            page=self,
        ) 
開發者ID:OpenTechFund,項目名稱:hypha,代碼行數:23,代碼來源:models.py

示例4: test_full_clear

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def test_full_clear(self):
        """
        Integration happy-path test that a model FileField can actually be set
        and cleared via a ModelForm.
        """
        class DocumentForm(forms.ModelForm):
            class Meta:
                model = Document
                fields = '__all__'

        form = DocumentForm()
        self.assertIn('name="myfile"', str(form))
        self.assertNotIn('myfile-clear', str(form))
        form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', b'content')})
        self.assertTrue(form.is_valid())
        doc = form.save(commit=False)
        self.assertEqual(doc.myfile.name, 'something.txt')
        form = DocumentForm(instance=doc)
        self.assertIn('myfile-clear', str(form))
        form = DocumentForm(instance=doc, data={'myfile-clear': 'true'})
        doc = form.save(commit=False)
        self.assertFalse(doc.myfile) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:24,代碼來源:tests.py

示例5: test_filefield_changed

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def test_filefield_changed(self):
        """
        The value of data will more than likely come from request.FILES. The
        value of initial data will likely be a filename stored in the database.
        Since its value is of no use to a FileField it is ignored.
        """
        f = FileField()

        # No file was uploaded and no initial data.
        self.assertFalse(f.has_changed('', None))

        # A file was uploaded and no initial data.
        self.assertTrue(f.has_changed('', {'filename': 'resume.txt', 'content': 'My resume'}))

        # A file was not uploaded, but there is initial data
        self.assertFalse(f.has_changed('resume.txt', None))

        # A file was uploaded and there is initial data (file identity is not dealt
        # with here)
        self.assertTrue(f.has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'})) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:22,代碼來源:test_filefield.py

示例6: make_entry_field

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def make_entry_field(self, fieldsubmission=None):
        f = forms.FileField(required=self.config['required'],
            label=self.config['label'],
            help_text=self.config['help_text'],
            widget=_ClearableFileInput())
        f.filesub = None
        if fieldsubmission:
            file_sub = fieldsubmission.file_sub()
            if file_sub:
                f.filesub = file_sub
                f.initial = file_sub.file_attachment
                f.initial.file_sub = fieldsubmission.file_sub()
        return f 
開發者ID:sfu-fas,項目名稱:coursys,代碼行數:15,代碼來源:other.py

示例7: __eq__

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def __eq__(self, other):
        # Older code may be expecting FileField values to be simple strings.
        # By overriding the == operator, it can remain backwards compatibility.
        if hasattr(other, 'name'):
            return self.name == other.name
        return self.name == other 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:8,代碼來源:files.py

示例8: __init__

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
        self._primary_key_set_explicitly = 'primary_key' in kwargs
        self._unique_set_explicitly = 'unique' in kwargs

        self.storage = storage or default_storage
        self.upload_to = upload_to
        if callable(upload_to):
            self.generate_filename = upload_to

        kwargs['max_length'] = kwargs.get('max_length', 100)
        super(FileField, self).__init__(verbose_name, name, **kwargs) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:13,代碼來源:files.py

示例9: check

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def check(self, **kwargs):
        errors = super(FileField, self).check(**kwargs)
        errors.extend(self._check_unique())
        errors.extend(self._check_primary_key())
        return errors 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:7,代碼來源:files.py

示例10: deconstruct

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def deconstruct(self):
        name, path, args, kwargs = super(FileField, self).deconstruct()
        if kwargs.get("max_length", None) == 100:
            del kwargs["max_length"]
        kwargs['upload_to'] = self.upload_to
        if self.storage is not default_storage:
            kwargs['storage'] = self.storage
        return name, path, args, kwargs 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:10,代碼來源:files.py

示例11: get_internal_type

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def get_internal_type(self):
        return "FileField" 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:4,代碼來源:files.py

示例12: get_prep_value

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def get_prep_value(self, value):
        "Returns field's value prepared for saving into a database."
        value = super(FileField, self).get_prep_value(value)
        # Need to convert File objects provided via a form to unicode for database insertion
        if value is None:
            return None
        return six.text_type(value) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:9,代碼來源:files.py

示例13: pre_save

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def pre_save(self, model_instance, add):
        "Returns field's value just before saving."
        file = super(FileField, self).pre_save(model_instance, add)
        if file and not file._committed:
            # Commit the file to storage prior to saving the model
            file.save(file.name, file, save=False)
        return file 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:9,代碼來源:files.py

示例14: contribute_to_class

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def contribute_to_class(self, cls, name, **kwargs):
        super(FileField, self).contribute_to_class(cls, name, **kwargs)
        setattr(cls, self.name, self.descriptor_class(self)) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:5,代碼來源:files.py

示例15: formfield

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import FileField [as 別名]
def formfield(self, **kwargs):
        defaults = {'form_class': forms.FileField, 'max_length': self.max_length}
        # If a file has been provided previously, then the form doesn't require
        # that a new file is provided this time.
        # The code to mark the form field as not required is used by
        # form_for_instance, but can probably be removed once form_for_instance
        # is gone. ModelForm uses a different method to check for an existing file.
        if 'initial' in kwargs:
            defaults['required'] = False
        defaults.update(kwargs)
        return super(FileField, self).formfield(**defaults) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:13,代碼來源:files.py


注:本文中的django.forms.FileField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。