sorlthumnail调整大小并保存

2024-05-16 02:06:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用sorl thumnail来调整视图中的图像大小,然后保存它并在调用get_thumnail()时获取IOError。我还需要知道如何保存调整大小的图像。抱歉,如果你认为这很愚蠢…我是新来的Django。在

在模型.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

在视图.py公司名称:

^{pr2}$

在预览.html公司名称:

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

在设置.py公司名称:

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

错误:

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) 

有人能帮我吗? 谢谢!在


Tags: djangofrompyimageimport名称视图models