本文整理汇总了Python中django.core.files.uploadedfile.UploadedFile.url方法的典型用法代码示例。如果您正苦于以下问题:Python UploadedFile.url方法的具体用法?Python UploadedFile.url怎么用?Python UploadedFile.url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.uploadedfile.UploadedFile
的用法示例。
在下文中一共展示了UploadedFile.url方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import url [as 别名]
def load(self, file_storage):
field_dict = self.data
field_dict = field_dict.copy()
tmp_name = field_dict.pop('tmp_name')
file_obj = UploadedFile(file=file_storage.open(tmp_name), **field_dict)
file_obj.url = file_storage.url(tmp_name)
return file_obj
示例2: value_from_datadict
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import url [as 别名]
def value_from_datadict(self, data, files, name):
# we cache the return value of this function, since it is called a
# bunch of time, and is expensive
if self._cached_value is self._sentinel:
upload = super().value_from_datadict(data, files, name)
if upload != FILE_INPUT_CONTRADICTION:
self.signed_path = data.get(self.signed_path_field_name(name), None)
data_uri = data.get(self.data_uri_field_name(name))
has_file = (upload or data_uri)
# the path to the cached uploaded file
path = None
# if we have a cache key, and no file, fetch the file from the cache
if self.signed_path and not has_file:
try:
path = self.signer.unsign(self.signed_path)
except BadSignature:
# False means the field value should be cleared, which
# is the best thing we can do in this case. It
# shouldn't happen anyways.
self.signed_path = ""
self._cached_value = None
return self._cached_value
elif has_file:
# we have a file, so write it to disk, just in case form validation fails
with NamedTemporaryFile(prefix="".join(CHOICES[x % 64] for x in os.urandom(16)), suffix=".jpg", dir=self.tmp_dir, delete=False) as f:
# write the uploaded file to disk, or the data from the dataURI
try:
if upload:
f.write(upload.read())
else:
f.write(b64decode(data_uri[data_uri.find(",")+1:]))
except Error:
pass
else:
path = f.name
self.signed_path = self.signer.sign(f.name)
if path:
upload = UploadedFile(open(path, "rb"), name=path, size=os.path.getsize(path))
# tack on a URL attribute so the parent Widget thinks it
# has an initial value
upload.url = settings.MEDIA_URL + os.path.relpath(upload.file.name, settings.MEDIA_ROOT)
self._cached_value = upload
return self._cached_value