【发布时间】:2015-08-06 16:44:15
【问题描述】:
我想在上传之前调整图像大小(枕头),我在下面写了代码但不起作用! 并得到错误:
/myapp/list/ 处的 AttributeError
_提交
请求方法:POST
请求网址:http://127.0.0.1:8000/myapp/list/ Django 版本:1.8 异常类型:AttributeError 异常值:
_提交
异常位置:
/usr/local/lib/python3.4/dist-packages/Pillow-2.8.1-py3.4-linux-x86_64.egg/PIL/Image.py
在 getattr 中,第 622 行 Python 可执行文件:/usr/bin/python3.4 Python 版本:3.4.0
views.py
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
imga = request.FILES['docfile']
size = (600, 400)
im = Image.open(imga)
imga = im.resize(size)
request.FILES['docfile'] = imga
newdoc = Document(docfile = request.FILES['docfile'], namefile=request.POST['namefile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
【问题讨论】:
-
与您的问题无关,而是与问题的标题有关:您知道您正在服务器端调整图像大小,因此技术上 在 上传之后(而不是之前)。
标签: python django python-imaging-library