Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

django - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...