Size: 1672
Comment:
|
Size: 3973
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 25: | Line 25: |
=== 썸네일 만들기 === [http://code.djangoproject.com/wiki/ThumbNails 썸네일 만드는 다양한 방법들]을 참고할 수 있는데, 그 가운데 [http://superjared.com/entry/django-quick-tips-2-image-thumbnails/ 원본저장시 하나의 썸네일을 동시저장]하는 방법이 간편한 방법이라 할만하다. 하지만, 1.0에서는 동작하지 아니함. 1.0을 위해서는 아래와 같아야 함. {{{#!python from django.db import models class Photo(models.Model): """ A photo for my site """ title = models.CharField(maxlength=50) photo = models.ImageField(upload_to="photos/") thumbnail = models.ImageField(upload_to="thumbnails/", editable=False) def save(self): from PIL import Image from cStringIO import StringIO from django.core.files.uploadedfile import SimpleUploadedFile # Set our max thumbnail size in a tuple (max width, max height) THUMBNAIL_SIZE = (50, 50) # Open original photo which we want to thumbnail using PIL's Image # object image = Image.open(self.photo.name) # Convert to RGB if necessary # Thanks to Limodou on DjangoSnippets.org # http://www.djangosnippets.org/snippets/20/ if image.mode not in ('L', 'RGB'): image = image.convert('RGB') # We use our PIL Image object to create the thumbnail, which already # has a thumbnail() convenience method that contrains proportions. # Additionally, we use Image.ANTIALIAS to make the image look better. # Without antialiasing the image pattern artifacts may result. image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) # Save the thumbnail temp_handle = StringIO() image.save(temp_handle, 'png') temp_handle.seek(0) # Save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1], temp_handle.read(), content_type='image/png') self.thunbnail.save(suf.name+'.png', suf, save=False) # Save this photo instance super(Photo, self).save() class Admin: pass def __str__(self): return self.title }}} |
[Django] 1.0 릴리즈(2008-09-04) 이후 바뀐 점들
참고정보
[http://docs.djangoproject.com/en/dev/releases/1.0-porting-guide/ 1.0으로 포팅하기]
[http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges 하위호환성 변화]
관련포스트
[http://www.hannal.net/blog/django-1_0_final_released/ 드디어 Django 1.0 정식판이 나왔습니다.]
- [wiki:Blog/330 Django 1.0 릴리즈, 새버전으로 갈아타기]
참고이슈들
form_for_model 이 없어짐
form_for_model 은 ModelForm을 서브클래싱하여 쓰라고 되어있으나 몇가지 문제가 있다.
다른 폼과 mixin 하여 쓸 수 없다. : metaclass conflict 메세지와 함께 안된다고 나온다. http://www.djangosnippets.org/snippets/703 을 써서 동작하게 할 수 있다.
mixin 대상이 둘 다 ModelForm 일 경우 또 동작하지 않는다. http://code.djangoproject.com/ticket/7018 로 보고가 되어있다. 임시방편은 티켓페이지 참고
모델객체 save할때 force_insert?
모델의 save를 오버라이드 했을 경우, force_insert=True 인수를 추가로 줘야한다. 이는 모델객체를 create 할때 update가 아닌 insert만 하고자 하는 의도. ([http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#createandget_or_createwillneverupdateexistingobjects 참고])
업로드파일의 경로변경
다음처럼 파일경로(path, url)가 바뀌였다.
model.get_filefield_filename() --> model.filefield.name
model.get_filefield_url() --> model.filefield.url
썸네일 만들기
[http://code.djangoproject.com/wiki/ThumbNails 썸네일 만드는 다양한 방법들]을 참고할 수 있는데, 그 가운데 [http://superjared.com/entry/django-quick-tips-2-image-thumbnails/ 원본저장시 하나의 썸네일을 동시저장]하는 방법이 간편한 방법이라 할만하다. 하지만, 1.0에서는 동작하지 아니함. 1.0을 위해서는 아래와 같아야 함.
1 from django.db import models
2
3 class Photo(models.Model):
4 """
5 A photo for my site
6 """
7 title = models.CharField(maxlength=50)
8 photo = models.ImageField(upload_to="photos/")
9 thumbnail = models.ImageField(upload_to="thumbnails/", editable=False)
10
11 def save(self):
12 from PIL import Image
13 from cStringIO import StringIO
14 from django.core.files.uploadedfile import SimpleUploadedFile
15
16 # Set our max thumbnail size in a tuple (max width, max height)
17 THUMBNAIL_SIZE = (50, 50)
18
19 # Open original photo which we want to thumbnail using PIL's Image
20 # object
21 image = Image.open(self.photo.name)
22
23 # Convert to RGB if necessary
24 # Thanks to Limodou on DjangoSnippets.org
25 # http://www.djangosnippets.org/snippets/20/
26 if image.mode not in ('L', 'RGB'):
27 image = image.convert('RGB')
28
29 # We use our PIL Image object to create the thumbnail, which already
30 # has a thumbnail() convenience method that contrains proportions.
31 # Additionally, we use Image.ANTIALIAS to make the image look better.
32 # Without antialiasing the image pattern artifacts may result.
33 image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
34
35 # Save the thumbnail
36 temp_handle = StringIO()
37 image.save(temp_handle, 'png')
38 temp_handle.seek(0)
39
40 # Save to the thumbnail field
41 suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1],
42 temp_handle.read(), content_type='image/png')
43 self.thunbnail.save(suf.name+'.png', suf, save=False)
44
45 # Save this photo instance
46 super(Photo, self).save()
47
48 class Admin:
49 pass
50
51 def __str__(self):
52 return self.title