English 中文(简体)
django - save a resize copy of image on upload (using admin site)
原标题:

I m building a site for a client that needs to support image uploads (an artist) through the admin interface. Since most of the images are pretty high-res, I wanted to create thumb copies of the image to display on the gallery page after the upload. The upload works great with the forms.ImageFile element, but I was looking for some ideas on how to do the actual resizing and and linking between the thumb and the true size images. I had an idea to hold model class for both an image and an image thumb:

from django.db import models

class Image(models.Model):
    """a true size image"""
    image = models.ImageFile(upload_to="images")
    desc = models.CharField(max_length=256)

    class Meta:
        db_table = "images"

class ImageThumb(models.Model):
    """"a thumbnail of an actual image"""
    real_image = models.ForeignKey( Image )
    image = models.ImageField(upload_to="images/thumbs")

    class Meta:
        db_table = "thumbs"

That part I m stuck on is how to resize the real image after upload (pil? how?), and I could probably use some polishing on my models - any help will be great. Thanks.

最佳回答

There s a great plugin called sorl-thumbnail that deals with thumbnail generation - don t bother doing it yourself. sorl-thumbnail is very configurable, so the chances are it ll do anything you want it to do.

If that doesn t work for you, then photologue is also very good (photologue is more tailored towards managing photo albums, rather than just plain thumbnail generation).

问题回答

See also easy-thumbnails and aino-convert. They might be a good bet since sorl-thumbnail might not be developed very actively from now on.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签