Django easy_thumbnails 学习心得
一些 Django 的easy_thumbnails 的学习心得 。
如果你不知道Django是什么,不知道easy_install是什么,请留步,别浪费时间往下读了。
GIt: https://github.com/SmileyChris/easy-thumbnails
model.py
class Images(models.Model): image = models.ImageField(upload_to='hello',blank=True)
admin.py:
admin.site.register(Images)
如果是用south管理数据库的,运行:
View Code
timest@timest-ThinkPad:~/django/pro$ python manage.py migrate easy_thumbnails Running migrations for easy_thumbnails: - Migrating forwards to 0015_auto__del_unique_thumbnail_name_storage_hash__add_unique_thumbnail_sou. > easy_thumbnails:0001_initial > easy_thumbnails:0002_filename_indexes > easy_thumbnails:0003_auto__add_storagenew > easy_thumbnails:0004_auto__add_field_source_storage_new__add_field_thumbnail_storage_new > easy_thumbnails:0005_storage_fks_null > easy_thumbnails:0006_copy_storage - Migration 'easy_thumbnails:0006_copy_storage' is marked for no-dry-run. > easy_thumbnails:0007_storagenew_fks_not_null > easy_thumbnails:0008_auto__del_field_source_storage__del_field_thumbnail_storage > easy_thumbnails:0009_auto__del_storage > easy_thumbnails:0010_rename_storage > easy_thumbnails:0011_auto__add_field_source_storage_hash__add_field_thumbnail_storage_hash > easy_thumbnails:0012_build_storage_hashes - Migration 'easy_thumbnails:0012_build_storage_hashes' is marked for no-dry-run. > easy_thumbnails:0013_auto__del_storage__del_field_source_storage__del_field_thumbnail_stora > easy_thumbnails:0014_auto__add_unique_source_name_storage_hash__add_unique_thumbnail_name_s > easy_thumbnails:0015_auto__del_unique_thumbnail_name_storage_hash__add_unique_thumbnail_sou - Loading initial data for easy_thumbnails. Installed 0 object(s) from 0 fixture(s)
否则运行:
python manager.py syncdb
settings.py:
THUMBNAIL_ALIASES = { '': { 'avatar': {'size': (50, 50), 'crop': True}, 'avatar_nocorp': {'size': (50,50), 'crop':False}, 'big' : {'size': (200,200), 'crop':True}, 'big_nocorp' : {'size': (200,200), 'crop':False}, 'huge': {'size': (900,900), 'crop':True}, 'huge_nocorp': {'size': (900,900), 'crop':False}, }, }
根据这个测试,就可以很明显的知道 crop 的作用 !如果crop = False , 如果图片太大,就不剪裁但同比例缩小!可以传一张长的和一张高的看看效果。
文档上写着,图片的质量只要在settings.py 中加上 THUMBNAIL_QUALITY = 85 就行。但是本地上测试,20 和 推荐的95 效果一样,而且图片大小也一样!
doc:
THUMBNAIL_QUALITY
Default: 85
The default quality level for JPG images on a scale from 1 (worst) to 95 (best). Technically, values up to 100 are allowed, but this is not recommended.
页面上显示:
<pre> <img src="{{image.image|thumbnail_url:'avatar'}}" /> <img src="{{image.image|thumbnail_url:'avatar_nocorp'}}" /> <img src="{{image.image|thumbnail_url:'big'}}" /> <img src="{{image.image|thumbnail_url:'big_nocorp'}}" /> <img src="{{image.image|thumbnail_url:'huge'}}" /> <img src="{{image.image|thumbnail_url:'huge_nocorp'}}" /> </pre>
不需要在src前面加上 {{MEDIA_URL}} , 因为默认就是这个,如需修改 ,配置:THUMBNAIL_MEDIA_ROOT
doc:
THUMBNAIL_MEDIA_ROOT
Default: ''
Used by easy-thumbnail’s default storage to locate where thumbnails are stored on the file system.
If not provided, Django’s standard MEDIA_ROOT setting is used.
THUMBNAIL_MEDIA_URL
Default: ''
Used by easy-thumbnail’s default storage to build the absolute URL for a generated thumbnail.
If not provided, Django’s standard MEDIA_URL setting is used.
另外还有一种方法也很简约:
models.py:
from easy_thumbnails.fields import ThumbnailerImageField class Imagess(models.Model): url = ThumbnailerImageField(upload_to='photos', blank=True)
前台显示:
<body bgcolor="#000"> {% load thumbnail %} <pre> <img src="{{ image.url.avatar.url}}" /> <img src="{{ image.url.avatar_nocorp.url}}" /> </pre> </body>