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
Comments
#61 may have some solutions for you |
sorl-thumbnail/sorl/thumbnail/images.py Lines 105 to 106 in aadb9fa
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. |
I updated the suggestion of @last-partizan to be able to use in development environment (without running 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,
) |
A library to create thumbnails and then dealing with static files is a major pain. Didn't expect that. |
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 thethumbnail
tag, so instead I need to use my own image from the static folder. Unfortunately it seemssorl
doesn't handle that, and I can't find any clean way to enable it.The text was updated successfully, but these errors were encountered: