Python django.conf.urls.static.static() Examples

The following are 19 code examples for showing how to use django.conf.urls.static.static(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.conf.urls.static , or try the search function .

Example 1
Project: luscan-devel   Author: blackye   File: static.py    License: GNU General Public License v2.0 6 votes vote down vote up
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),
    ) 
Example 2
Project: GTDWeb   Author: lanbing510   File: urls.py    License: GNU General Public License v2.0 5 votes vote down vote up
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 
Example 3
Project: bioforum   Author: reBiocoder   File: urls.py    License: MIT License 5 votes vote down vote up
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 
Example 4
Project: gro-api   Author: OpenAgricultureFoundation   File: urls.py    License: GNU General Public License v2.0 5 votes vote down vote up
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 
Example 5
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: urls.py    License: MIT License 5 votes vote down vote up
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 
Example 6
Project: openhgsenti   Author: drexly   File: urls.py    License: Apache License 2.0 5 votes vote down vote up
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 
Example 7
Project: python2017   Author: bpgc-cte   File: urls.py    License: MIT License 5 votes vote down vote up
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 
Example 8
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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)) 
Example 9
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example 10
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self._old_views_urlpatterns = urls.urlpatterns[:]
        urls.urlpatterns += static('/media/', document_root=media_dir) 
Example 11
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_prefix(self):
        self.assertEqual(static('test')[0].pattern.regex.pattern, '^test(?P<path>.*)$') 
Example 12
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_debug_off(self):
        """No URLs are served if DEBUG=False."""
        self.assertEqual(static('test'), []) 
Example 13
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_special_prefix(self):
        """No URLs are served if prefix contains '://'."""
        self.assertEqual(static('http://'), []) 
Example 14
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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)) 
Example 15
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example 16
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self._old_views_urlpatterns = urls.urlpatterns[:]
        urls.urlpatterns += static('/media/', document_root=media_dir) 
Example 17
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_prefix(self):
        self.assertEqual(static('test')[0].pattern.regex.pattern, '^test(?P<path>.*)$') 
Example 18
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_empty_prefix(self):
        with self.assertRaisesMessage(ImproperlyConfigured, 'Empty static prefix not permitted'):
            static('') 
Example 19
Project: djongo   Author: nesdis   File: test_static.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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'), [])