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

Use of static files with the template tag #568

Open
timmyomahony opened this issue Oct 4, 2018 · 4 comments
Open

Use of static files with the template tag #568

timmyomahony opened this issue Oct 4, 2018 · 4 comments

Comments

@timmyomahony
Copy link

There is no way to use static files with the thumbnail tag.

An example use-case for cropping static files is placeholder images. I know the THUMBNAIL_DUMMY setting exists, but this doesn't dynamically crop the dummy image when used with the thumbnail tag, so instead I need to use my own image from the static folder. Unfortunately it seems sorl doesn't handle that, and I can't find any clean way to enable it.

@tisdall
Copy link

tisdall commented Jan 4, 2019

#61 may have some solutions for you

@last-partizan
Copy link

last-partizan commented Nov 11, 2021

elif hasattr(file_, 'storage'):
self.storage = file_.storage

These two lines allows us to use following hack.

from sorl.thumbnail import get_thumbnail
from django.core.files.storage import FileSystemStorage
from django.conf import settings


class StaticPath(str):

    def __new__(cls, path: str, storage: FileSystemStorage):
        obj = super().__new__(cls, path)
        obj.storage = storage
        return obj


storage = FileSystemStorage(location=settings.STATIC_ROOT, base_url=settings.STATIC_URL)


def get_static_thumbnail(file_: str, *args, **kwargs):
    return get_thumbnail(
        StaticPath(file_, storage),
        *args,
        **kwargs,
    )

but i don't think it should be even this complicated. Maybe this code should be integrated into sorl-thumbnail. Or maybe sorl-thumbnail should provide better workaround for this case.

I like to hear what you think guys, maybe other ideas how do you see possible soultion.

And if enough people is interested, we (maybe I) should make PR.

@benjaoming
Copy link
Contributor

benjaoming commented Jan 23, 2022

I updated the suggestion of @last-partizan to be able to use in development environment (without running collectstatic).

This might not be what everyone wants!

from django.core.files.storage import FileSystemStorage
from django.contrib.staticfiles import finders
from django import template
from sorl.thumbnail import get_thumbnail


# This storage can be considered unsafe if you start feeding it variable paths
# from your templates, so be a bit careful.
storage = FileSystemStorage(location="/")


@register.simple_tag(takes_context=False)
def get_static_thumbnail(file_: str, geometry: str, *args, **kwargs):
    # Resolve the full disk path with static file finders
    disk_path = finders.find(file_)
    if disk_path:
        return get_thumbnail(
            StaticPath(disk_path, storage),
            geometry,
            *args,
            **kwargs,
        )

@alfonsrv
Copy link

A library to create thumbnails and then dealing with static files is a major pain. Didn't expect that.

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

5 participants