我是Django(和Python)的新手,在嘗試使用其他人的應用程序之前,我一直在嘗試自己解決一些問題。我無法理解Django(或Python)做事的方式。我試圖解決的是如何調整圖像的大小,一旦它被上傳。我有我的模型很好地建立並插入到管理,並且將圖像上傳罰款的目錄:用django調整圖片尺寸?
from django.db import models
# This is to list all the countries
# For starters though, this will be just United Kingdom (GB)
class Country(models.Model):
name = models.CharField(max_length=120, help_text="Full name of country")
code = models.CharField(max_length=2, help_text="This is the ISO 3166 2-letter country code (see: http://www.theodora.com/country_digraphs.html)")
flag = models.ImageField(upload_to="images/uploaded/country/", max_length=150, help_text="The flag image of the country.", blank=True)
class Meta:
verbose_name_plural = "Countries"
def __unicode__(self):
return self.name
現在我在與正在該文件,使一個新的文件爲縮略圖,麻煩的事情。就像我說的,我想知道如何去做,而不使用別人的應用程序(現在)。我已得到DjangoSnippets驗證碼:
from PIL import Image
import os.path
import StringIO
def thumbnail(filename, size=(50, 50), output_filename=None):
image = Image.open(filename)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)
# get the thumbnail data in memory.
if not output_filename:
output_filename = get_default_thumbnail_filename(filename)
image.save(output_filename, image.format)
return output_filename
def thumbnail_string(buf, size=(50, 50)):
f = StringIO.StringIO(buf)
image = Image.open(f)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)
o = StringIO.StringIO()
image.save(o, "JPEG")
return o.getvalue()
def get_default_thumbnail_filename(filename):
path, ext = os.path.splitext(filename)
return path + '.thumb.jpg'
...但這最終弄得我......因爲我不知道這是如何「在符合」我的Django應用程序?真的,這是僅僅製作已成功上傳圖像的縮略圖的最佳解決方案嗎?任何人都可能向我展示一種像我這樣的初學者可以學會如何正確做到這一點的良好,堅實和體面的方式?就像在知道在哪裏放置這種代碼(models.py?forms.py?...)以及它如何在上下文中工作一樣? ...我只需要一點幫助理解和解決這個問題。
謝謝!
謝謝葉節點,我我聽說過這個,我會看看它。但我希望能夠嘗試和理解首先做事情的基本方式,希望能夠讓我更好地理解它是如何工作的......也許這不值得這樣做? ...但我認爲這樣做是值得的,以使我成爲更好的Python/Django開發人員? – littlejim84 2009-07-22 12:47:09
如果您想了解詳細信息,請查看其他答案,但請記住它是針對用戶端表單的。 – leafnode 2009-07-22 12:48:41
它是deaaaaaaaaaaaaad – titus 2011-12-03 06:19:10