Django-thumbnail

mseo39ยท2021๋…„ 2์›” 17์ผ
0

django

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

๐Ÿ“šlikelion 9th์˜ django ๊ฐ•์˜ ํ•„๊ธฐ

๐Ÿ“‘Django

thumbnail

  • ์ธ๋„ค์ผ ํŒŒ์ผ ์ง€์ • ์šฉ์ด
  • ํŒŒ์ผ ์šฉ๋Ÿ‰ ๊ด€๋ฆฌ
  • ํŒŒ์ผ ๋ถ€๋ฅ˜์— ํšจ์œจ์ 

1. media ์„ค์ •

์ด ๋งํฌ๋กœ ๋“ค์–ด๊ฐ€์„œ media๋ถ€๋ถ„ ๋”ฐ๋ผํ•˜๊ธฐ๐Ÿ‘‰ media ๋ฐ”๋กœ๊ฐ€๊ธฐ

โœmodels.py๋Š” ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋„๋ก ํ•œ๋‹ค
class Pictures(models.Model):
    text = models.TextField()
    image= models.ImageField(upload_to="blogimg")

2. imagekit ์„ค์น˜ํ•˜๊ธฐ

๐Ÿ‘‰ํ„ฐ๋ฏธ๋„์— ์ž‘์„ฑ
โœ
pip install pillow 
pip install django-imagekit

3. setting.py

๐Ÿ‘‰setting.py์— ์žˆ๋Š” INSTALLED_APPS ์•ˆ์— ์ž‘์„ฑ
โœ
INSTALLED_APPS = [

    'imagekit',
]

4. models.py ์ˆ˜์ •

๐Ÿ‘‰models.py ์ˆ˜์ •
โœ
from django.db import models
from imagekit.models import ImageSpecField #์ธ๋„ค์ผ ์ง€์ •
from imagekit.processors import ResizeToFill #์ธ๋„ค์ผ ํฌ๊ธฐ

class Pictures(models.Model):
    text = models.TextField()
    image= models.ImageField(upload_to="blogimg")
    image_thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(120,60)]) #(๊ฐ€๋กœ,์„ธ๋กœ)

5. html

๐Ÿ‘‰settings.py์— ์žˆ๋Š” INSTALLED_APPS ๊ด„ํ˜ธ ์•ˆ์— ์ž‘์„ฑ
โœ
  {% for blog in blog.all %}
    <img src="{{blog.image.url}}" width="500">
    <br>
    {{blog.text}}
    <br><br>
    <img src="{{blog.image_thumbnail.url}}">
  {% endfor %}

0๊ฐœ์˜ ๋Œ“๊ธ€