1. ํ์ํ package ์ค์นํ๊ธฐ
1-1. ImageField
๋จ์ ImageField๋ฅผ ํ์ฉํ๊ธฐ ์ํด์๋ pillow
ํจํค์ง๊ฐ ๋ฐ๋์ ํ์ํ๋ค.
1-2. resizing
Resizing์ ์ํด์๋ pilkit, django-imagekit ํจํค์ง๊ฐ ํ์ํ๋ค.
Copy pip install pilkit django-imagekit
2. model์ image column ์ ์
posts > models.py
Copy class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
image = models.ImageField(blank=True)
# DB ์ ์ฅ x, ํธ์ถํ๊ฒ ๋๋ฉด ์๋ผ์ ํํ
image_thumbnail = ImageSpecField(source='image',
processors=[ResizeToFill(300, 300)],
format='JPEG',
options={'quality': 60})
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#์ข์์ ๊ธฐ๋ฅ
like_users = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='like_posts')
3. view์ request.FILES
์ถ๊ฐ
posts > views.py
Copy @login_required
def create(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False) # ์ถ๊ฐํจ
post.user = request.user
post.save()
return redirect('posts:index')
messages.warning(request, 'Please check the form submitted')
else:
form = PostForm()
context = {
'form': form
}
return render(request, 'posts/forms.html', context)
def update(request):
if request.method == 'POST':
form = CustomUserChangeForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('posts:index')
else:
form = CustomUserChangeForm(instance=request.user)
context ={
'form':form
}
return render(request, 'accounts/update.html', context)
4. settings.py
์์
Copy # media file ์ ์ฅ ๊ฒฝ๋ก
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
5. urls.py
์ ๊ฒฝ๋ก ์ถ๊ฐ
Copy from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls')),
path('accounts/', include('accounts.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
6. Templates ์์
forms.html
Copy <form action="" method="POST" enctype="multipart/form-data">
detail.html
Copy <!--image ์ถ๋ ฅ์ฉ-->
<img src="{{post.image.url}}"/>
<img src="{{post.image_thumbnail.url}}"/>
migrations
Copy $ python manage.py makemigrations
# default๊ฐ ์์ด NOT NULL๋ฅผ ์ง์ => ๊ธฐ์กด ๋ ์ฝ๋์ ๊ฐ์ด ํ์ํ๋ค.
You are trying to add a non-nullable field 'image' to article without a default; we can't do that (the database needs something to populate existing rows).
# 2๊ฐ์ง ์ต์
์ ์
Please select a fix:
# 1) ๋ํดํธ ๊ฐ์ ์ง๊ธ ์ค์ => python console
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
# 2) ์ข
๋ฃํ๊ณ ์ง์ models.py์ default ์ค์
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
django-imagekit library
Image ๋ด๋ถ์ ์ผ๋ก thumbnail์ ๋ง๊ฒ ์๋ฅด๊ธฐ
Download & usage
https://github.com/matthewwithanm/django-imagekit
Installation
Copy pip install pilkit django-imagekit
์๋ณธ ์์ฒด๋ฅผ ์๋ผ์ ์ ์ฅ
Copy ProcessedImageField(upload_to='avatars',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 60})