djangoDjango日记python学习专题

使用 Django-thumbs 生成缩略图

2016-12-28  本文已影响876人  与蟒唯舞

Django-thumbs is the easiest way to create thumbnails for your ImageFields with Django.You can integrate it easily in your code and it works with any StorageBackend.

特征

安装

$ pip install django-thumbs

要点

范例

from django.db import models
from django_thumbs.db.models import ImageWithThumbsField

class Person(models.Model):
    photo = ImageWithThumbsField(upload_to='images', sizes=((125, 125), (200, 200)))
    second_photo = ImageWithThumbsField(upload_to='images')

In this example we have a Person model with 2 image fields.

You can see the field second_photo doesn't have a sizes attribute. This field works exactly the same way as a normal ImageField.

The field photo has a sizes attribute specifying desired sizes for the thumbnails. This field works the same way as ImageField but it also creates the desired thumbnails when uploading a new file and deletes the thumbnails when deleting the file.

With ImageField you retrieve the URL for the image with:someone.photo.url With ImageWithThumbsField you retrieve it the same way. You also retrieve the URL for every thumbnail specifying its size:In this example we use someone.photo.url_125x125 and someone.photo.url_200x200 to get the URL of both thumbnails.

卸载

At any time you can go back and use ImageField again without altering the database or anything else. Just replace ImageWithThumbsField with ImageField again and make sure you delete the sizes attribute. Everything will work the same way it worked before using django-thumbs. Just remember to delete generated thumbnails in the case you don't want to have them anymore.

我的项目

新建 App
startapp base
settings.py
INSTALLED_APPS = (
    ...
    'base'
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
models.py
# coding:utf-8
from django.db import models
from django_thumbs.db.models import ImageWithThumbsField
import os, datetime, uuid


def generate_filename(instance, filename):
    """
    安全考虑,生成随机文件名
    """
    directory_name = datetime.datetime.now().strftime('photos/%Y/%m/%d')
    filename = uuid.uuid4().hex + os.path.splitext(filename)[-1]
    return os.path.join(directory_name, filename)


class Photo(models.Model):
    name = models.CharField('名称', max_length=10)
    photo = ImageWithThumbsField('照片', upload_to=generate_filename, sizes=((150, 150),))

    def __unicode__(self):
        return self.photo.url_150x150

    class Meta:
        verbose_name_plural = verbose_name = '照片'
admin.py
# coding: utf-8
from django.contrib import admin
from .models import Photo


class PhotoAdmin(admin.ModelAdmin):
    list_display = ('name', 'photo',)


admin.site.register(Photo, PhotoAdmin)

后台成功上传图片效果图:


>>> from base.models import Photo
>>> ps = Photo.objects.all()
>>> ps
[<Photo: /media/photos/2016/12/27/02e374320dae43888fc75c4d3041f93b.150x150.jpg>]

通过控制台由此可得上传图片缩略图的访问地址为:
http://127.0.0.1:8000/media/photos/2016/12/27/02e374320dae43888fc75c4d3041f93b.150x150.jpg

注意 :

要想成功访问图片,需要在 urls.py 里面添加以下内容:

from django.conf.urls import url
from django.conf import settings

urlpatterns = [
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT})
]
上一篇下一篇

猜你喜欢

热点阅读