本文整理汇总了Python中django.conf.urls.static.static方法的典型用法代码示例。如果您正苦于以下问题:Python static.static方法的具体用法?Python static.static怎么用?Python static.static使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.conf.urls.static
的用法示例。
在下文中一共展示了static.static方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: static
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def static(prefix, view='django.views.static.serve', **kwargs):
"""
Helper function to return a URL pattern for serving files in debug mode.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
"""
# No-op if not in debug mode or an non-local prefix
if not settings.DEBUG or (prefix and '://' in prefix):
return []
elif not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
return patterns('',
url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
)
示例2: staticfiles_urlpatterns
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def staticfiles_urlpatterns(prefix=None):
"""
Helper function to return a URL pattern for serving static files.
"""
if prefix is None:
prefix = settings.STATIC_URL
return static(prefix, view=serve)
# Only append if urlpatterns are empty
示例3: staticfiles_urlpatterns
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def staticfiles_urlpatterns(prefix=None):
"""
Helper function to return a URL pattern for serving static files.
"""
if prefix is None:
prefix = settings.STATIC_URL
return static(prefix, view=serve)
# Only append if urlpatterns are empty
示例4: get_current_urls
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def get_current_urls():
urls = BaseRouter.get_instance().urls + [
url(r'^auth/', include(auth_patterns)),
url(r'^auth/registration/', include(auth_registration_patterns)),
url(r'^docs/', include('rest_framework_swagger.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
import debug_toolbar
urls.append(url(r'^__debug__/', include(debug_toolbar.urls)))
return urls
示例5: test_serve
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_serve(self):
"The static view can serve static media"
media_files = ['file.txt', 'file.txt.gz', '%2F.txt']
for filename in media_files:
response = self.client.get('/%s/%s' % (self.prefix, quote(filename)))
response_content = b''.join(response)
file_path = path.join(media_dir, filename)
with open(file_path, 'rb') as fp:
self.assertEqual(fp.read(), response_content)
self.assertEqual(len(response_content), int(response['Content-Length']))
self.assertEqual(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
示例6: test_chunked
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_chunked(self):
"The static view should stream files in chunks to avoid large memory usage"
response = self.client.get('/%s/%s' % (self.prefix, 'long-line.txt'))
first_chunk = next(response.streaming_content)
self.assertEqual(len(first_chunk), FileResponse.block_size)
second_chunk = next(response.streaming_content)
response.close()
# strip() to prevent OS line endings from causing differences
self.assertEqual(len(second_chunk.strip()), 1449)
示例7: setUp
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def setUp(self):
super().setUp()
self._old_views_urlpatterns = urls.urlpatterns[:]
urls.urlpatterns += static('/media/', document_root=media_dir)
示例8: test_prefix
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_prefix(self):
self.assertEqual(static('test')[0].pattern.regex.pattern, '^test(?P<path>.*)$')
示例9: test_debug_off
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_debug_off(self):
"""No URLs are served if DEBUG=False."""
self.assertEqual(static('test'), [])
示例10: test_special_prefix
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_special_prefix(self):
"""No URLs are served if prefix contains '://'."""
self.assertEqual(static('http://'), [])
示例11: test_empty_prefix
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_empty_prefix(self):
with self.assertRaisesMessage(ImproperlyConfigured, 'Empty static prefix not permitted'):
static('')
示例12: test_special_prefix
# 需要导入模块: from django.conf.urls import static [as 别名]
# 或者: from django.conf.urls.static import static [as 别名]
def test_special_prefix(self):
"""No URLs are served if prefix contains a netloc part."""
self.assertEqual(static('http://example.org'), [])
self.assertEqual(static('//example.org'), [])