调整大小和保存 [英] sorl-thumnail resizing and saving

查看:149
本文介绍了调整大小和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sorl-thumnail调整视图中的图像大小,然后保存它并在调用get_thumnail()时获取IOError。另外我还需要知道如何保存调整大小的图像。对不起,如果你认为这个傻...我是Django的新人。



Models.py:



<$ p $从django.db导入模型的$ p code从django.forms导入的
ImportForm
from sorl.thumbnail import ImageField

class BasicModel(models.Model):
name = models.CharField(max_length = 200)
dob = models.DateField()
photo = ImageField(upload_to ='sample')

class BasicModelForm ModelForm):
class Meta:
model = BasicModel

Views.py:

  def BasicView(request):
如果request.method =='POST':
form = BasicModelForm(request.POST,request.FILES)
如果form.is_valid():
im = get_thumbnail(request.FILES ['photo'],'100x100',crop ='center',quality = 99)
data = form.save()
return preview(request,data.id,im)
else:
form = BasicModelForm()
return render_to_response(unnamed.html,{'form':form},context_instance = RequestContext(request))

def preview(request,id,im) :
obj = get_object_or_404(BasicModel,pk = id)
返回render_to_response(preview.html,{'obj':obj,'im':im})

preview.html:

  {obj.name}} 
{{obj.dob}}
{%load thumbnail%}
{%thumbnail im100x100as image%}
< img src ={{image.url}}width ={{image.width}}height ={{image.height}}>
{%endthumbnail%}

Settings.py:

  MEDIA_ROOT ='/ home / nirmal / try / files /'
MEDIA_URL ='http:// localhost:8000 / files /'

错误:

 异常类型:IOError 
异常值:
[Errno 2]没有这样的文件或目录:u'/ home / nirmal / try / files / wp.jpg'
异常位置:/ usr / local / lib / python2.7 / dist-packages / django / core / files / storage.py在_open,第159行
Traceback:im = get_thumbnail(request.FILES ['photo'],'100x100' ,crop ='center',quality = 99)

有人可以帮助我吗?
谢谢!

解决方案

这里不能使用request.FILES ['photo'],因为上传的文件可以在记忆或其他地方。先将文件保存到文件系统,然后使用get_thumbnail。例如,在form.save()返回后,您可以在对象上调用它。


I'm trying to use sorl-thumnail to resize the image in the views then saving it and getting IOError while calling get_thumnail(). Also I need to know how to save the resized image. Sorry if you consider this silly..I'm new to Django.

Models.py:

from django.db import models
from django.forms import ModelForm
from sorl.thumbnail import ImageField

class BasicModel(models.Model):
    name = models.CharField(max_length=200)
    dob = models.DateField()
    photo = ImageField(upload_to='sample')

class BasicModelForm(ModelForm):
    class Meta:
            model = BasicModel

Views.py:

def BasicView(request):
    if request.method == 'POST':
            form = BasicModelForm(request.POST, request.FILES)
            if form.is_valid():
                    im = get_thumbnail(request.FILES['photo'], '100x100', crop='center', quality=99)
                    data = form.save()
                    return preview(request, data.id, im)
    else:
            form = BasicModelForm()
    return render_to_response("unnamed.html", {'form': form}, context_instance=RequestContext(request))

def preview(request, id, im):
    obj = get_object_or_404(BasicModel, pk=id)
    return render_to_response("preview.html", {'obj': obj, 'im': im})

preview.html:

{{ obj.name }}
{{ obj.dob }}
{% load thumbnail %}
{% thumbnail im "100x100" as image %}
<img src="{{ image.url }}" width="{{ image.width }}" height="{{ image.height }}">
{% endthumbnail %}

Settings.py:

MEDIA_ROOT = '/home/nirmal/try/files/'
MEDIA_URL = 'http://localhost:8000/files/'

Errors:

Exception Type: IOError
Exception Value:    
[Errno 2] No such file or directory: u'/home/nirmal/try/files/wp.jpg'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/files/storage.py in _open, line 159
Traceback: im = get_thumbnail(request.FILES['photo'], '100x100', crop='center', quality=99) 

Could anyone help me on this? Thanks!

解决方案

You can't use request.FILES['photo'] here because uploaded file can be in memory or somewhere else. Save this file to filesystem first, then use get_thumbnail. For example, you can call it on your objects after it's returned by form.save().

这篇关于调整大小和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆