Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorl-thumbnail bad url's

I setup sorl-thumbnail according to instructions, but none of the images are appearing when I try to use the templatetags in my app.

It appears that the url's are not valid, but it's not clear what additional configuration is needed.

A image like like this is generated:

<img src="cache/e5/25/e5253a328b9130ecd7d820893f44b0e6.jpg" width="100" height="100">

How does "cache/..." get resolved to a request for an image? These requests are relative to my application, not to sorl-thumbnail:

[31/May/2011 07:13:05] "GET /myapp/cache/e5/25/cache/e5/25/cache/00/73/0073095ee4b968b45386ef3fec4f389c.jpg HTTP/1.1" 200 1004

Here are the relevant lines in settings.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the  admin:                                          
    'django.contrib.admin',
    'mysite.myapp',
    'sorl.thumbnail',
)

CACHES = {
#    'default': {                                                                           
#        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',                          
#        'LOCATION': 'cache',                                                               
#    }                                                                                    
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

# URL that handles the media served from MEDIA_ROOT. Make sure to use a                     
# trailing slash if there is a path component (optional in other cases).                    
# Examples: "http://media.lawrence.com", "http://example.com/media/"                        
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use   a              
# trailing slash.                                                                           
# Examples: "http://foo.com/media/", "/media/".                                             
ADMIN_MEDIA_PREFIX = '/media/'

This is the code in my template:

{% thumbnail auction.item.image "100x100" crop="center" as im %}
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

The image is definitely getting uploaded (I checked the directory specified in upload_to), and when I was using the filesystem cache they were getting stored in the directory cache/ relative to my app. I changed it to use memcache to see if that would help.

like image 224
Nate Reed Avatar asked May 31 '11 12:05

Nate Reed


1 Answers

You need to configure MEDIA_URL correctly. The "url" attribute of an ImageFile is basically just a pass-through from the underlying storage backend. For out-of-the-box Django, the upload_to path is appended to MEDIA_URL to generate the URL for a FileField.

What you have: '' + 'cache/e5/25/e5253a328b9130ecd7d820893f44b0e6.jpg'
What you want: '/media/' + 'cache/e5/25/e5253a328b9130ecd7d820893f44b0e6.jpg'

Note: you would need to make sure that MEDIA_URL is aliased/mapped to whatever directory Django is uploading your files to (MEDIA_ROOT).

----- EDIT ----
See the following links to the source of the default Django storage backend. https://code.djangoproject.com/browser/django/tags/releases/1.3/django/core/files/storage.py#L154
https://code.djangoproject.com/browser/django/tags/releases/1.3/django/core/files/storage.py#L240

like image 144
shadfc Avatar answered Oct 13 '22 20:10

shadfc