Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serving Static Files #61

Closed
DarwinSurvivor opened this issue Sep 20, 2011 · 3 comments
Closed

Serving Static Files #61

DarwinSurvivor opened this issue Sep 20, 2011 · 3 comments

Comments

@DarwinSurvivor
Copy link

I can't find any way to thumbnail a static (in STATIC_ROOT) file. It only seems to work on external (http://....) and media (in MEDIA_ROOT) files.

This is very helpful when the output size of a static image is not always the same (be it through user preferences, theming, etc)

@sorl
Copy link
Collaborator

sorl commented Sep 20, 2011

You need to use some storage that uses STATIC_ROOT, for example you can use 'django.core.files.storage.FileSystemStorage' and instantiate with location=settings.STATIC_ROOT and base_url=settings.STATIC_URL, please continue on stackoverflow as this is not a bug.

@sorl sorl closed this as completed Sep 20, 2011
@marcinn
Copy link

marcinn commented Oct 9, 2016

After five years still there is no possibility to generate thumbs from static files without changing the DEFAULT storage, because of https://github.com/mariocesar/sorl-thumbnail/blob/master/sorl/thumbnail/base.py#L100
StackOverflow will not help due to implementation.

Any chances to FIX that? Passing custom storage instance via get_thumbnail() will be enough.

@marcinn
Copy link

marcinn commented Oct 9, 2016

Here is an example of a patch marcinn@61557b4

EDIT: sorry, it seems that output from thumbnail.url is valid, but when retrieved from cached.url it is prefixed with /media/ instead of /static/. There is somethin' more broken inside.

EDIT (2): there is a design issue in deserializing storage code - the storage class is loaded and instantiated with defaults instead of using existing instance. In the other words - sorl.thumbnail does not reuse storage instance but creates own (configured differently). As a workaround we can inherit from FileSystemStorage and define custom initializer.

After applying it anyone can easily generate thumbnails for anything.
It is possible to create a custom template tag like this one:

from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django import template
from sorl.thumbnail.templatetags.thumbnail import ThumbnailNode


register = template.Library()

class StaticThumbnailStorage(FileSystemStorage):
    def __init__(self, *args, **kw):
        super(StaticThumbnailStorage, self).__init__(
            *args, location='/path/to/custom/static/'
            base_url=settings.STATIC_URL, **kw)

storage = StaticThumbnaikStorage()

class StaticThumbnailNode(ThumbnailNode):
    def _get_thumbnail(self, file_, geometry, **options):
        options['storage'] = storage
        return super(StaticThumbnailNode, self)._get_thumbnail(
                file_, geometry, **options)


@register.tag
def static_thumbnail(parser, token):
    return StaticThumbnailNode(parser, token)

Please note that settings do not need to be changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants