Sorl-缩略图错误网址

发布于 2024-11-10 12:15:53 字数 2285 浏览 0 评论 0原文

我根据说明设置了 sorl-thumbnail,但是当我尝试在应用程序中使用模板标签时,没有任何图像出现。

该 URL 似乎无效,但尚不清楚需要哪些额外配置。

生成这样的图像:

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

“cache/...”如何解析为图像请求?这些请求与我的应用程序相关,而不是 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

以下是 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/'

这是我模板中的代码:

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

图像肯定正在上传(我检查了 upload_to 中指定的目录),并且何时我正在使用文件系统缓存,它们存储在相对于我的应用程序的目录 cache/ 中。我将其更改为使用内存缓存,看看是否有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-11-17 12:15:53

您需要正确配置MEDIA_URL。 ImageFile 的 "url" 属性 基本上只是来自底层存储后端。对于开箱即用的 Django, upload_to 路径附加到 MEDIA_URL 以生成 FileField 的 URL。

你有什么:'' + 'cache/e5/25/e5253a328b9130ecd7d820893f44b0e6.jpg'
你想要什么: '/media/' + 'cache/e5/25/e5253a328b9130ecd7d820893f44b0e6.jpg'

注意:你需要确保 MEDIA_URL 是别名/映射到 Django 将文件上传到的任何目录 (MEDIA_ROOT)。

----- 编辑 ----
请参阅以下默认 Django 存储后端源的链接。
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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文