画像フィールドを持つ標準の Django フォームがあります。画像をアップロードするときは、画像が 300px × 300px 以下であることを確認したいと思います。これが私のコードです:
def post(request):
if request.method == 'POST':
instance = Product(posted_by=request.user)
form = ProductModelForm(request.POST or None, request.FILES or None)
if form.is_valid():
new_product = form.save(commit=False)
if 'image' in request.FILES:
img = Image.open(form.cleaned_data['image'])
img.thumbnail((300, 300), Image.ANTIALIAS)
# this doesnt save the contents here...
img.save(new_product.image)
# ..because this prints the original width (2830px in my case)
print new_product.image.width
私が直面している問題はImage
、タイプを ImageField タイプのタイプに変換する方法がはっきりしないことです。