本文整理汇总了Python中django.core.files.storage.default_storage.url方法的典型用法代码示例。如果您正苦于以下问题:Python default_storage.url方法的具体用法?Python default_storage.url怎么用?Python default_storage.url使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.storage.default_storage
的用法示例。
在下文中一共展示了default_storage.url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_render
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_render(self):
w = widgets.AdminFileWidget()
self.assertHTMLEqual(
w.render('test', self.album.cover_art),
'<p class="file-upload">Currently: <a href="%(STORAGE_URL)salbums/'
r'hybrid_theory.jpg">albums\hybrid_theory.jpg</a> '
'<span class="clearable-file-input">'
'<input type="checkbox" name="test-clear" id="test-clear_id"> '
'<label for="test-clear_id">Clear</label></span><br>'
'Change: <input type="file" name="test"></p>' % {
'STORAGE_URL': default_storage.url(''),
},
)
self.assertHTMLEqual(
w.render('test', SimpleUploadedFile('test', b'content')),
'<input type="file" name="test">',
)
示例2: test_readonly_fields
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_readonly_fields(self):
"""
File widgets should render as a link when they're marked "read only."
"""
self.client.force_login(self.superuser)
response = self.client.get(reverse('admin:admin_widgets_album_change', args=(self.album.id,)))
self.assertContains(
response,
'<div class="readonly"><a href="%(STORAGE_URL)salbums/hybrid_theory.jpg">'
r'albums\hybrid_theory.jpg</a></div>' % {'STORAGE_URL': default_storage.url('')},
html=True,
)
self.assertNotContains(
response,
'<input type="file" name="cover_art" id="id_cover_art">',
html=True,
)
response = self.client.get(reverse('admin:admin_widgets_album_add'))
self.assertContains(
response,
'<div class="readonly"></div>',
html=True,
)
示例3: student_view_data
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def student_view_data(self):
"""
Inform REST api clients about original file location and it's "freshness".
Make sure to include `student_view_data=scormxblock` to URL params in the request.
"""
if self.scorm_file and self.scorm_file_meta:
return {
'last_modified': self.scorm_file_meta.get('last_updated', ''),
'scorm_data': default_storage.url(self._file_storage_path()),
'size': self.scorm_file_meta.get('size', 0),
'index_page': self.path_index_page,
}
return {}
示例4: save_user_file
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def save_user_file(user, file, subdir, hashed=False):
"""
Save user media file into sub-dir. \
The `hashed`` param prevents file duplication
"""
file_path = utils.generate_filename(file, hashed=hashed)
name = os.path.join('spirit', subdir, str(user.pk), file_path)
name = default_storage.save(name, file)
file.name = os.path.basename(file_path)
file.url = default_storage.url(name)
return file
示例5: test_url
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_url(self):
self.assertTrue(
self.storage.url("my_file.txt").endswith("/test/my_file.txt"))
self.storage.expiration_secs = 360
# has some query-string
self.assertTrue("/test/my_file.txt?" in self.storage.url("my_file.txt"))
示例6: test_url_unsafe_chars
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_url_unsafe_chars(self):
name = "my?file <foo>.txt"
expected = "/test/my%3Ffile%20%3Cfoo%3E.txt"
self.assertTrue(
self.storage.url(name).endswith(expected))
# has some query-string
self.storage.expiration_secs = 360
self.assertTrue("{}?".format(expected) in self.storage.url(name))
示例7: test_url_custom_endpoint
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_url_custom_endpoint(self):
storage = azure_storage.AzureStorage()
storage.is_emulated = True
storage.custom_domain = 'foobar:123456'
self.assertTrue(storage.url("my_file.txt").startswith('https://foobar:123456/'))
示例8: test_template_media_file
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_template_media_file(self):
t = Template('{{ file_url }}')
rendered = t.render(Context({
'file_url': default_storage.url('foo.txt')})).strip()
self.assertTrue(
"https://127.0.0.1:10000/devstoreaccount1/test_private/foo.txt?" in rendered)
self.assertTrue("&" in rendered)
# check static files still work
t = Template(
'{% load static from staticfiles %}'
'{% static "foo.txt" %}')
self.assertEqual(
t.render(Context({})).strip(),
"https://127.0.0.1:10000/devstoreaccount1/test/foo.txt")
示例9: test_name_clean_issue_609
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_name_clean_issue_609(self):
"""
Should strip special characters when using the default storage
"""
simple_file = SimpleFileModel()
simple_file.foo_file = SimpleUploadedFile(
name='foo%?:;~bar.txt',
content=b'foo content')
simple_file.save()
self.assertEqual(simple_file.foo_file.name, 'foo_uploads/foobar.txt')
self.assertTrue('foobar.txt' in simple_file.foo_file.url)
示例10: test_save_small_file
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_save_small_file(self):
with self.save_file():
logging.info("content type: %s", default_storage.content_type("test.txt"))
self.assertEqual(default_storage.open("test.txt").read(), b"test")
self.assertEqual(requests.get(default_storage.url("test.txt")).content, b"test")
示例11: test_save_big_file
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_save_big_file(self):
with self.save_file(content=b"test" * 1000):
logging.info("content type: %s", default_storage.content_type("test.txt"))
self.assertEqual(default_storage.open("test.txt").read(), b"test" * 1000)
self.assertEqual(requests.get(default_storage.url("test.txt")).content, b"test" * 1000)
示例12: test_url
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_url(self):
with self.save_file(name="folder/test?+123.txt"):
url = default_storage.url("folder/test?+123.txt")
logging.info("url: %s", url)
response = requests.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b"test")
self.assertEqual(response.headers['Content-Type'], "text/plain")
auth = oss2.Auth(settings.OSS_ACCESS_KEY_ID, settings.OSS_ACCESS_KEY_SECRET)
bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.OSS_BUCKET_NAME)
if bucket.get_bucket_acl().acl == 'private':
self.assertEqual(url.find('.txt?') > 0, True)
else:
self.assertEqual(url.find('.txt?') > 0, False)
示例13: test_url_cn
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_url_cn(self):
objname = to_unicode("本地文件名.txt")
logging.info("objname: %s", objname)
data = oss2.compat.to_bytes('我的座右铭')
with self.save_file(objname, data) as name:
self.assertEqual(name, objname)
url = default_storage.url(objname)
logging.info("url: %s", url)
response = requests.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, data)
self.assertEqual(response.headers['Content-Type'], "text/plain")
示例14: test_static_url
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def test_static_url(self):
with self.save_file(storage=staticfiles_storage):
url = staticfiles_storage.url("test.txt")
logging.info("url: %s", url)
response = requests.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b"test")
self.assertEqual(response.headers['Content-Type'], "text/plain")
示例15: get_thumbnail_picture_url
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import url [as 别名]
def get_thumbnail_picture_url(self):
url = ""
picture_path, thumbnail_path = self.get_picture_paths()
if thumbnail_path:
url = (storage.url(thumbnail_path)
if storage.exists(thumbnail_path)
else self.picture.url)
return url